forked from a-sync/arma3pregen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
30 lines (27 loc) · 1.18 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
const fs = require('fs');
const { createServer } = require('http');
const { URL } = require('url');
const backend = require('./backend/index.js');
const APP_HOST = process.env.app_host || process.env.APP_HOST || '0.0.0.0';
const APP_PORT = parseInt(process.env.app_port || process.env.APP_PORT || '80', 10);
createServer((req, res) => {
// console.log('DBG: %j %j', (new Date()), req.url);
const reqUrl = new URL(req.url || '', 'http://localhost');
const pn = reqUrl.pathname.slice(-1) === '/' ? reqUrl.pathname.slice(0, -1) : reqUrl.pathname;
const p = pn.split('/').pop() || 'index.html';
if (['index.html', 'main.css', 'main.js'].includes(p)) {
let ct = 'text/html';
if (p === 'main.css') ct = 'text/css';
else if (p === 'main.js') ct = 'text/javascript';
res.writeHead(200, { 'Content-Type': ct });
fs.createReadStream('./' + p).pipe(res);
}
else if (p === 'backend') {
backend(req, res);
}
else {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('<html><head></head><body>404 💢</body></html>');
}
}).listen(APP_PORT);
console.log('Web service started %s:%s', APP_HOST, APP_PORT);