-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
41 lines (36 loc) · 979 Bytes
/
Copy pathserver.js
File metadata and controls
41 lines (36 loc) · 979 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
34
35
36
37
38
39
40
41
// Simple HTTP server for the web UI
import http from 'http';
import fs from 'fs';
import path from 'path';
const PORT = 3000;
const MIME = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
};
const server = http.createServer((req, res) => {
let filePath;
if (req.url === '/' || req.url === '/index.html') {
filePath = './web/index.html';
} else if (req.url.startsWith('/data/')) {
filePath = '.' + req.url;
} else {
filePath = './web' + req.url;
}
const ext = path.extname(filePath);
const contentType = MIME[ext] || 'application/octet-stream';
fs.readFile(filePath, (err, content) => {
if (err) {
res.writeHead(404);
res.end('Not found');
return;
}
res.writeHead(200, { 'Content-Type': contentType });
res.end(content);
});
});
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});