-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.mjs
More file actions
36 lines (32 loc) · 1.06 KB
/
serve.mjs
File metadata and controls
36 lines (32 loc) · 1.06 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
import { createServer } from 'node:http';
import { readFile } from 'node:fs/promises';
import { join, extname, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const MIME = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'application/javascript',
'.json': 'application/json',
'.woff2': 'font/woff2',
'.md': 'text/plain',
'.yml': 'text/plain',
'.yaml': 'text/plain',
'.png': 'image/png',
'.svg': 'image/svg+xml',
};
const server = createServer(async (req, res) => {
let path = req.url === '/' ? '/templates/cv-template.html' : decodeURIComponent(req.url);
const filePath = join(__dirname, path);
try {
const data = await readFile(filePath);
const ext = extname(filePath);
res.writeHead(200, { 'Content-Type': MIME[ext] || 'application/octet-stream' });
res.end(data);
} catch {
res.writeHead(404);
res.end('Not found');
}
});
const port = process.env.PORT || 3000;
server.listen(port, () => console.log(`Serving on http://localhost:${port}`));