-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserve.mjs
More file actions
34 lines (31 loc) · 994 Bytes
/
serve.mjs
File metadata and controls
34 lines (31 loc) · 994 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
import { createServer } from 'http';
import { readFile } from 'fs/promises';
import { join, extname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const PORT = 3000;
const MIME = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'application/javascript',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.pdf': 'application/pdf',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.ico': 'image/x-icon',
};
createServer(async (req, res) => {
let path = decodeURIComponent(req.url === '/' ? '/index.html' : req.url);
try {
const data = await readFile(join(__dirname, path));
res.writeHead(200, { 'Content-Type': MIME[extname(path)] || 'application/octet-stream' });
res.end(data);
} catch {
res.writeHead(404);
res.end('Not found');
}
}).listen(PORT, () => console.log(`Serving at http://localhost:${PORT}`));