-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
51 lines (44 loc) · 1.39 KB
/
Copy pathserver.js
File metadata and controls
51 lines (44 loc) · 1.39 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
const http = require("http");
const fs = require("fs");
const path = require("path");
const root = __dirname;
const port = Number(process.env.PORT || 3000);
const mimeTypes = {
".html": "text/html; charset=utf-8",
".json": "application/json; charset=utf-8",
".js": "text/javascript; charset=utf-8",
".css": "text/css; charset=utf-8",
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".svg": "image/svg+xml",
".mp4": "video/mp4",
".wdp": "application/octet-stream"
};
function safePath(urlPath) {
const decoded = decodeURIComponent(urlPath.split("?")[0]);
const requested = decoded === "/" ? "/chatsim.html" : decoded;
const fullPath = path.normalize(path.join(root, requested));
return fullPath.startsWith(root) ? fullPath : null;
}
const server = http.createServer((req, res) => {
const filePath = safePath(req.url || "/");
if (!filePath) {
res.writeHead(403);
res.end("Forbidden");
return;
}
fs.stat(filePath, (statErr, stats) => {
if (statErr || !stats.isFile()) {
res.writeHead(404);
res.end("Not found");
return;
}
const ext = path.extname(filePath).toLowerCase();
res.writeHead(200, { "Content-Type": mimeTypes[ext] || "application/octet-stream" });
fs.createReadStream(filePath).pipe(res);
});
});
server.listen(port, () => {
console.log(`ChatSim running at http://localhost:${port}/chatsim.html`);
});