25 lines
797 B
JavaScript
25 lines
797 B
JavaScript
import { createServer } from "node:http";
|
|
import { readFile } from "node:fs/promises";
|
|
const types = {
|
|
js: "application/javascript",
|
|
css: "text/css",
|
|
html: "text/html",
|
|
};
|
|
createServer(async (req, res) => {
|
|
try {
|
|
const path = new URL(req.url, "http://localhost").pathname;
|
|
const name = path === "/" ? "index.html" : path.slice(1);
|
|
if (!/^[a-z.]+$/.test(name)) throw new Error("Invalid path");
|
|
res.setHeader(
|
|
"Content-Type",
|
|
types[name.split(".").at(-1)] ?? "text/plain",
|
|
);
|
|
res.setHeader("Cache-Control", "no-store");
|
|
res.end(await readFile(new URL(`../dist/${name}`, import.meta.url)));
|
|
} catch {
|
|
res.writeHead(404);
|
|
res.end("Not found");
|
|
}
|
|
}).listen(5221, "127.0.0.1", () =>
|
|
console.log("Primordial Soup: http://127.0.0.1:5221"),
|
|
);
|