-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
54 lines (48 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
53
54
// Starts the server from the command line
const fs = require('fs');
const https = require('https');
const http = require('http');
const { program } = require('commander');
program.option('-d, --dev');
program.parse();
const opts = program.opts();
const app = require('./app.js');
// HTTP -> HTTPS redirect
if (!opts.dev) {
app.enable('trust proxy');
app.use((req, res, next) => {
req.secure ? next() : res.redirect('https://' + req.headers.host + req.url)
});
}
(async () => {
const onStart = () => {
console.log('Server Started');
};
let config = null;
try {
config = require('./config.json');
} catch (__) {
fs.writeFileSync('./config.json', JSON.stringify({
key: '',
cert: '',
ca: ''
}, null, 4));
config = require('./config.json');
}
if (opts.dev) {
http.createServer(app).listen(3000, onStart);
} else {
try {
https.createServer({
key: fs.readFileSync(config.key, 'utf8'),
cert: fs.readFileSync(config.cert, 'utf8'),
ca: fs.readFileSync(config.ca, 'utf8')
}, app).listen(443, onStart);
http.createServer(app).listen(80, onStart);
} catch (e) {
console.error(e);
reject(e);
process.exit(1);
}
}
})();