forked from robterrell/web-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
56 lines (45 loc) · 1.34 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
const https = require('https');
const url = require('url');
const fs = require('fs');
const options = {
key: fs.readFileSync('example/devlocal.info.key'),
cert: fs.readFileSync('example/devlocal.info.cert'),
};
const mimes = {
css : 'text/css',
html : 'text/html',
js : 'application/javascript',
wasm : 'application/wasm',
};
const paths = {
css : './example',
html : './example',
js : './src',
wasm : './src',
};
function zeroPad(num) {
return num < 10 ? '0' + num : num;
}
function timeStamp() {
const d = new Date();
return `${zeroPad(d.getMonth() + 1)}-${zeroPad(d.getDate())} ` +
`${zeroPad(d.getHours())}:${zeroPad(d.getMinutes())}:${zeroPad(d.getSeconds())}`;
}
https.createServer(options, (req, res) => {
const parsed = url.parse(req.url);
const fileName = parsed.pathname === '/' ? '/index.html' : parsed.pathname;
const fileType = fileName.split('.').slice(-1)[0];
const fullPath = (paths[fileType] || '.') + fileName;
let code = 404;
let color = '\x1b[31m';
let body = '404';
if (fs.existsSync(fullPath)) {
code = 200;
color = '\x1b[32m';
body = fs.readFileSync(fullPath);
}
console.log(`${color}%s\x1b[0m %s`, code, `[${timeStamp()}] ${fullPath}`);
res.writeHead(code, {'Content-Type': mimes[fileType] || 'text/plain'});
res.end(body);
}).listen(443);
console.log('Development server started on port 443');