Repository navigation
Expand file tree
/
Copy pathserver.mjs
More file actions
55 lines (48 loc) · 1.82 KB
/
Copy pathserver.mjs
File metadata and controls
55 lines (48 loc) · 1.82 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
import { createServer } from "node:http";
import { readFile } from "node:fs/promises";
import { statSync } from "node:fs";
import { dirname, extname, join, normalize, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const root = dirname(fileURLToPath(import.meta.url));
const port = Number(process.env.PORT || 4173);
const host = process.env.HOST || "0.0.0.0";
const mimeTypes = {
".html": "text/html; charset=utf-8",
".css": "text/css; charset=utf-8",
".js": "text/javascript; charset=utf-8",
".json": "application/json; charset=utf-8",
".svg": "image/svg+xml; charset=utf-8",
".ico": "image/x-icon",
".txt": "text/plain; charset=utf-8"
};
function safePath(urlPath) {
const clean = normalize(decodeURIComponent(urlPath.split("?")[0])).replace(/^(\.\.[/\\])+/, "");
const fullPath = resolve(join(root, clean === "/" ? "index.html" : clean));
return fullPath.startsWith(root) ? fullPath : join(root, "index.html");
}
const server = createServer(async (req, res) => {
try {
let filePath = safePath(req.url || "/");
try {
if (statSync(filePath).isDirectory()) {
filePath = join(filePath, "index.html");
}
} catch {
filePath = join(root, "index.html");
}
const data = await readFile(filePath);
const type = mimeTypes[extname(filePath)] || "application/octet-stream";
res.writeHead(200, {
"Content-Type": type,
"Cache-Control": "no-store"
});
res.end(data);
} catch (error) {
res.writeHead(500, { "Content-Type": "text/plain; charset=utf-8" });
res.end(`Rollwright server error:\n${error instanceof Error ? error.message : String(error)}`);
}
});
server.listen(port, host, () => {
console.log(`Rollwright is running at http://localhost:${port}`);
console.log(`LAN access uses this machine's local IP with port ${port}.`);
});