-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
57 lines (48 loc) · 1.46 KB
/
Copy pathserver.mjs
File metadata and controls
57 lines (48 loc) · 1.46 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
50
51
52
53
54
55
56
57
// Copied, with modification, from https://stackoverflow.com/a/29046869/6286797
import http from 'node:http';
import url from 'node:url';
import fs from 'node:fs';
import path from 'node:path';
const port = process.env.PORT ?? 22608;
http.createServer(function (req, res) {
console.log(`${req.method} ${req.url}`);
let parsedUrl = url.parse(req.url);
if (parsedUrl.pathname === '/') {
parsedUrl = url.parse('/timer.html');
}
let pathname = `serve${parsedUrl.pathname}`;
const ext = path.parse(pathname).ext;
const map = {
'.ico': 'image/x-icon',
'.html': 'text/html',
'.js': 'text/javascript',
'.mjs': 'text/javascript',
'.json': 'application/json',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.wav': 'audio/wav',
'.ogg': 'audio/ogg',
'.mp3': 'audio/mpeg',
'.svg': 'image/svg+xml',
};
fs.exists(pathname, function (exist) {
if(!exist) {
// if the file is not found, return 404
res.statusCode = 404;
res.end(`File ${pathname} not found!`);
return;
}
fs.readFile(pathname, function(err, data){
if(err){
res.statusCode = 500;
res.end(`Error getting the file: ${err}.`);
} else {
// if the file is found, set Content-type and send data
res.setHeader('Content-type', map[ext] || 'text/plain' );
res.end(data);
}
});
});
}).listen(port);
console.log(`Server listening at http://localhost:${port}`);