Serve static files with `node-static`.

pull/25/head
Pontus Persson 2017-12-08 15:28:09 +01:00 committed by Pontus Alexander
parent 7bd1bf148b
commit 8f411acee6
2 changed files with 8 additions and 10 deletions

View File

@ -15,6 +15,7 @@
"author": "", "author": "",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"node-static": "^0.7.10",
"ws": "^3.3.2" "ws": "^3.3.2"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,7 +1,9 @@
const HTTPS_PORT = 8443; const HTTPS_PORT = 8443;
const fs = require('fs'); const fs = require('fs');
const https = require('https'); const https = require('https');
const static = require('node-static');
const WebSocket = require('ws'); const WebSocket = require('ws');
const WebSocketServer = WebSocket.Server; const WebSocketServer = WebSocket.Server;
@ -11,20 +13,15 @@ const serverConfig = {
cert: fs.readFileSync('cert.pem'), cert: fs.readFileSync('cert.pem'),
}; };
const file = new static.Server('./client');
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
// Create a server for the client html page // Create a server for the client html page
var handleRequest = function(request, response) { var handleRequest = function(request, response) {
// Render the single client html file for any request the HTTP server receives request.addListener('end', () => {
console.log('request received: ' + request.url); file.serve(request, response);
}).resume();
if(request.url === '/') {
response.writeHead(200, {'Content-Type': 'text/html'});
response.end(fs.readFileSync('client/index.html'));
} else if(request.url === '/webrtc.js') {
response.writeHead(200, {'Content-Type': 'application/javascript'});
response.end(fs.readFileSync('client/webrtc.js'));
}
}; };
var httpsServer = https.createServer(serverConfig, handleRequest); var httpsServer = https.createServer(serverConfig, handleRequest);