-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
82 lines (66 loc) · 2.6 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require ('colors');
const os = require ('os');
const express = require ('express');
const bodyParser = require ('body-parser');
const path = require ('path');
const app = express ();
var http = require('http');
var https = require('https');
var fs = require('fs');
const config = {
server: {
port: 8080,
sslPort: 8443
},
datetime: {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
locale: 'de-DE',
format: {
hour : 'numeric',
minute: 'numeric',
second: 'numeric',
hour12: false
}
}
};
function getTime () {
return new Intl.DateTimeFormat(config.datetime.locale, config.datetime.format).format (new Date ());
}
app.use (function logRequestedResources (req, res, next) {
if (req.method === 'GET') {
// console.log (`[${getTime ()}] Serving requested file ${path.basename(req.url) || 'index.html'}`.dim);
}
next ();
});
app.use (express.static (path.resolve (__dirname, 'app')));
app.use ('/node_modules', express.static (path.resolve (__dirname, 'node_modules')));
app.use (bodyParser.text ());
app.post ('/api/log', function logError (req) {
const messageParts = req.body.split (os.EOL);
const errorMessage = messageParts[0];
const filenameTranspiled = messageParts[1].replace (/^.+((http.+\d\d:\d\d).+$)/, (a, b, url) => url).replace (/^.+\/(.+)$/, (a, file) => file);
const line = filenameTranspiled.replace (/^.+:(\d\d):.+$/, (a, line) => line);
const column = filenameTranspiled.replace (/^.+\d\d:(\d\d)$/, (a, col) => col);
const filename = filenameTranspiled.replace(/^(.+js).+$/, (a, file) => file);
const message = `[${getTime ()}] ${errorMessage.bold}, ${filename} line ${line}, column ${column}`;
console.log (message.red);
});
var httpServer = app.listen (config.server.port, function () {
console.log (`App is listening on http://localhost:${config.server.port}`);
});
// we need SSL when using web sensors: https://aghassi.github.io/ssl-using-express-4/
var sslOptions = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem'),
passphrase: 'devoxx4kids'
};
var httpsServer = https.createServer(sslOptions, app).listen(config.server.sslPort, function () {
console.log (`App is listening on https://localhost:${config.server.sslPort}`);
});
// to make sure
require('./app/multiplayer/server')(app, httpsServer);
require('./app/multiplayer/server')(app, httpServer);
const starttime = Date.now();
app.get ('/version', function (req, res) {
res.send(starttime + '');
});