-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
42 lines (37 loc) · 1.1 KB
/
Copy pathserver.js
File metadata and controls
42 lines (37 loc) · 1.1 KB
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
const http = require('http');
const fs = require('fs');
const path = require('path');
const mime = {
'.html': 'text/html',
'.js': 'text/javascript',
'.mjs': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.webmanifest': 'application/manifest+json',
'.svg': 'image/svg+xml',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.ico': 'image/x-icon'
};
const base = __dirname;
const port = 8001;
http.createServer((req, res) => {
let url = decodeURIComponent(req.url.split('?')[0]);
let p = path.join(base, url);
if (url === '/' || url === '') p = path.join(base, 'index.html');
// Block path traversal
if (!p.startsWith(base)) {
res.writeHead(403); res.end('Forbidden'); return;
}
fs.readFile(p, (e, d) => {
if (e) { res.writeHead(404); res.end('Not found'); return; }
const ext = path.extname(p).toLowerCase();
res.writeHead(200, {
'Content-Type': mime[ext] || 'application/octet-stream',
'Cache-Control': 'no-cache'
});
res.end(d);
});
}).listen(port, () => {
console.log(`Cosmogenesis serving on http://localhost:${port}`);
});