-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
33 lines (29 loc) · 840 Bytes
/
Copy pathserver.js
File metadata and controls
33 lines (29 loc) · 840 Bytes
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
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 8090;
const DIR = __dirname;
const MIME = {
'.html': 'text/html',
'.js': 'application/javascript',
'.json': 'application/json',
'.css': 'text/css',
'.png': 'image/png',
'.svg': 'image/svg+xml',
};
http.createServer((req, res) => {
const url = decodeURIComponent(req.url === '/' ? '/heatmap.html' : req.url);
const filePath = path.join(DIR, url);
const ext = path.extname(filePath);
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404);
res.end('Not found: ' + url);
return;
}
res.writeHead(200, { 'Content-Type': MIME[ext] || 'application/octet-stream' });
res.end(data);
});
}).listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});