-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevserver.js
More file actions
49 lines (44 loc) · 1.35 KB
/
Copy pathdevserver.js
File metadata and controls
49 lines (44 loc) · 1.35 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
43
44
45
46
47
48
49
const http = require('http');
const fs = require('fs');
const path = require('path');
const ROOT = __dirname;
const PORT = 3000;
const MIME = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.mp3': 'audio/mpeg',
'.wav': 'audio/wav',
'.json': 'application/json',
'.ico': 'image/x-icon',
'.svg': 'image/svg+xml',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.wasm': 'application/wasm'
};
const server = http.createServer((req, res) => {
let url = decodeURIComponent(req.url.split('?')[0]);
if (url === '/') url = '/index.html';
const filePath = path.join(ROOT, url);
const ext = path.extname(filePath).toLowerCase();
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found: ' + url);
return;
}
res.writeHead(200, {
'Content-Type': MIME[ext] || 'application/octet-stream',
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'no-store, no-cache, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
});
res.end(data);
});
});
server.listen(PORT, () => {
console.log('Dev server running at http://localhost:' + PORT);
});