-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (48 loc) · 1.49 KB
/
Copy pathserver.js
File metadata and controls
55 lines (48 loc) · 1.49 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
const http = require("http");
const fs = require("fs");
const path = require("path");
const root = __dirname;
const port = Number(process.env.PORT || 4180);
const types = {
".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",
".webmanifest": "application/manifest+json; charset=utf-8",
".wasm": "application/wasm",
};
http
.createServer((req, res) => {
let urlPath;
try {
urlPath = decodeURIComponent(new URL(req.url, `http://localhost:${port}`).pathname);
} catch {
res.writeHead(400);
res.end("Bad request");
return;
}
const filePath = path.resolve(root, `.${urlPath === "/" ? "/index.html" : urlPath}`);
const relative = path.relative(root, filePath);
if (relative.startsWith("..") || path.isAbsolute(relative)) {
res.writeHead(403);
res.end("Forbidden");
return;
}
fs.readFile(filePath, (err, content) => {
if (err) {
res.writeHead(404);
res.end("Not found");
return;
}
res.writeHead(200, {
"Content-Type": types[path.extname(filePath)] || "application/octet-stream",
"Cross-Origin-Opener-Policy": "same-origin",
"Cross-Origin-Embedder-Policy": "require-corp",
});
res.end(content);
});
})
.listen(port, () => {
console.log(`OpenHWP Studio running at http://localhost:${port}`);
});