22 lines
1.0 KiB
JavaScript
22 lines
1.0 KiB
JavaScript
import http from "node:http";
|
|
import { readFile } from "node:fs/promises";
|
|
import { extname, join, normalize } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const root = fileURLToPath(new URL(".", import.meta.url));
|
|
const types = { ".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".css": "text/css; charset=utf-8" };
|
|
|
|
http.createServer(async (req, res) => {
|
|
const path = new URL(req.url, "http://x").pathname;
|
|
if (path === "/healthz") { res.writeHead(200).end("ok"); return; }
|
|
const rel = normalize(path === "/" ? "/index.html" : path).replace(/^(\.\.[/\\])+/, "");
|
|
const file = join(root, rel);
|
|
if (!file.startsWith(root) || rel === "/index.js" || rel === "/server.js") { res.writeHead(404).end("Not found"); return; }
|
|
try {
|
|
const body = await readFile(file);
|
|
res.writeHead(200, { "content-type": types[extname(file)] ?? "application/octet-stream" }).end(body);
|
|
} catch {
|
|
res.writeHead(404).end("Not found");
|
|
}
|
|
}).listen(Number(process.env.PORT) || 3000);
|