-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-server.js
More file actions
69 lines (59 loc) · 1.91 KB
/
Copy pathdev-server.js
File metadata and controls
69 lines (59 loc) · 1.91 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
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env node
const http = require("http");
const fs = require("fs");
const path = require("path");
const HOST = "127.0.0.1";
const PORT = Number(process.env.PORT || 5173);
const ROOT = __dirname;
const MIME_TYPES = {
".html": "text/html; charset=utf-8",
".css": "text/css; charset=utf-8",
".js": "application/javascript; charset=utf-8",
".json": "application/json; charset=utf-8",
".svg": "image/svg+xml",
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".gif": "image/gif",
".ico": "image/x-icon",
".txt": "text/plain; charset=utf-8"
};
function safeResolve(urlPath) {
const cleanPath = decodeURIComponent(urlPath.split("?")[0]);
const relPath = cleanPath === "/" ? "/index.html" : cleanPath;
const resolved = path.normalize(path.join(ROOT, relPath));
if (!resolved.startsWith(ROOT)) return null;
return resolved;
}
const server = http.createServer((req, res) => {
const targetPath = safeResolve(req.url || "/");
if (!targetPath) {
res.writeHead(403, { "Content-Type": "text/plain; charset=utf-8" });
res.end("Forbidden");
return;
}
fs.stat(targetPath, (statErr, stats) => {
if (statErr) {
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
res.end("Not Found");
return;
}
const filePath = stats.isDirectory()
? path.join(targetPath, "index.html")
: targetPath;
const ext = path.extname(filePath).toLowerCase();
const contentType = MIME_TYPES[ext] || "application/octet-stream";
fs.readFile(filePath, (readErr, content) => {
if (readErr) {
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
res.end("Not Found");
return;
}
res.writeHead(200, { "Content-Type": contentType });
res.end(content);
});
});
});
server.listen(PORT, HOST, () => {
console.log(`[WATON_WEB] Dev server running at http://${HOST}:${PORT}`);
});