-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
52 lines (44 loc) · 1.36 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const https = require('https');
const path = require('path');
const fs = require('fs');
const log = require('./lib/utils/log');
const currentDir = path.dirname(fs.realpathSync(__filename));
const serveStatic = require('serve-static');
const serveStaticFilesMiddleware = serveStatic(path.join(currentDir, 'client'), {
'index': ['index.html', 'index.htm']
});
// to create self-signed certificate and key run `npm run create-ssl`
const key = fs.readFileSync(path.join(currentDir, 'ssl/private/server.key'));
const cert = fs.readFileSync(path.join(currentDir, 'ssl/certs/server.crt'));
const options = {
key: key,
cert: cert,
requestCert: false,
rejectUnauthorized: false
};
var server = https.createServer(options, function (req, res) {
if (req.signaling) {
return;
}
res.addListener('finish', function () {
log.write('serve-static', req.connection.remoteAddress, res.statusCode, req.url);
});
serveStaticFilesMiddleware(req, res, function (err) {
if (err) {
log.write('serve-static', log.red+'error'+log.reset, err);
res.writeHead(500, {"Content-Type": "text/plain"});
res.write("500 Internal Server Error\n");
res.end();
} else {
res.writeHead(404, {"Content-Type": "text/plain"});
res.write("404 Not Found\n");
res.end();
}
});
});
server.listen(8000);
const signaling = require('./lib/signaling');
signaling(server, {
xhr: true,
ws: true
});