-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
80 lines (70 loc) · 2.49 KB
/
server.js
File metadata and controls
80 lines (70 loc) · 2.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// ─── Dev server ───────────────────────────────────────────────────────────────
// This server is ONLY used for local development.
// In production the app is hosted on GitHub Pages (pure static).
// There is no backend — all data lives in a GitHub repo accessed via the
// GitHub REST API directly from the browser.
const http = require("http");
const fs = require("fs");
const path = require("path");
const PORT = process.env.PORT || 3000;
const PUBLIC = path.join(__dirname, "public");
const MIME = {
".html": "text/html; charset=utf-8",
".css": "text/css; charset=utf-8",
".js": "application/javascript; charset=utf-8",
".json": "application/json",
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".gif": "image/gif",
".svg": "image/svg+xml",
".ico": "image/x-icon",
".woff": "font/woff",
".woff2": "font/woff2",
".ttf": "font/ttf",
".map": "application/json",
};
const server = http.createServer((req, res) => {
// Strip query strings
let urlPath = req.url.split("?")[0];
// Default to index.html
if (urlPath === "/" || urlPath === "") urlPath = "/index.html";
const filePath = path.join(PUBLIC, urlPath);
// Security: prevent path traversal
if (!filePath.startsWith(PUBLIC + path.sep) && filePath !== PUBLIC) {
res.writeHead(403);
res.end("Forbidden");
return;
}
fs.readFile(filePath, (err, data) => {
if (err) {
if (err.code === "ENOENT") {
// SPA fallback: serve index.html for unknown paths
fs.readFile(path.join(PUBLIC, "index.html"), (err2, indexData) => {
if (err2) {
res.writeHead(500);
res.end("Internal Server Error");
return;
}
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
res.end(indexData);
});
} else {
res.writeHead(500);
res.end("Internal Server Error");
}
return;
}
const ext = path.extname(filePath).toLowerCase();
const contentType = MIME[ext] || "application/octet-stream";
res.writeHead(200, { "Content-Type": contentType });
res.end(data);
});
});
server.listen(PORT, () => {
console.log(`\n🔥 Pyroclasm dev server running at http://localhost:${PORT}`);
console.log(` Serving: ${PUBLIC}`);
console.log(
` Note: this server is for local dev only. Deploy public/ to GitHub Pages.\n`,
);
});