-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
34 lines (29 loc) · 1.17 KB
/
server.js
File metadata and controls
34 lines (29 loc) · 1.17 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
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 80;
const ROOT = path.join(__dirname);
const MIME = {
'.html': 'text/html', '.css': 'text/css', '.js': 'application/javascript',
'.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg',
'.gif': 'image/gif', '.svg': 'image/svg+xml', '.ico': 'image/x-icon',
'.woff': 'font/woff', '.woff2': 'font/woff2', '.mp4': 'video/mp4',
};
const server = http.createServer((req, res) => {
let filePath = path.join(ROOT, req.url === '/' ? 'index.html' : req.url);
filePath = path.normalize(filePath);
if (!filePath.startsWith(ROOT)) { res.writeHead(403); res.end(); return; }
fs.stat(filePath, (err, stats) => {
if (err || !stats.isFile()) {
// SPA fallback
filePath = path.join(ROOT, 'index.html');
}
const ext = path.extname(filePath);
const mime = MIME[ext] || 'application/octet-stream';
res.writeHead(200, { 'Content-Type': mime, 'Cache-Control': 'public, max-age=3600' });
fs.createReadStream(filePath).pipe(res);
});
});
server.listen(PORT, '0.0.0.0', () => {
console.log(`Raptor Predictor serving on http://0.0.0.0:${PORT}`);
});