-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
28 lines (25 loc) · 755 Bytes
/
Copy pathserver.js
File metadata and controls
28 lines (25 loc) · 755 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
import http from 'node:http';
import fs from 'node:fs';
import { extname } from 'node:path';
const contentType = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
};
const server = http.createServer((req, res) => {
const filename = req.url.trim().slice(1) || 'index.html';
console.log(filename);
if (!fs.existsSync(filename)) {
res.writeHead(404);
res.end();
return;
}
const file = fs.readFileSync(filename);
const type = contentType[extname(filename)] || 'text/plain';
const headers = type ? { 'content-type': type } : {};
res.writeHead(200, headers);
res.write(file);
res.end();
})
server.listen(8080);
console.log('Listening on http://localhost:8080');