CODE · 1.1 KB
scripts/serve-qa-export.mjs
Workspace snapshot · 08/31 23:51
import { createReadStream } from "node:fs";
import { stat } from "node:fs/promises";
import { createServer } from "node:http";
import { extname, join } from "node:path";
const root = new URL("../output/qa-export/", import.meta.url).pathname.replace(/^\/(.:)/u, "$1");
const types = { ".html": "text/html; charset=utf-8", ".svg": "image/svg+xml", ".png": "image/png" };
const server = createServer(async (request, response) => {
if (request.method === "POST" && request.url === "/thanks.html") {
request.resume();
response.writeHead(303, { Location: "/thanks.html" });
response.end();
return;
}
const pathname = request.url === "/" ? "/index.html" : String(request.url || "/index.html").split("?")[0];
const file = join(root, pathname.replace(/^\/+/, ""));
try {
await stat(file);
response.writeHead(200, { "Content-Type": types[extname(file)] || "application/octet-stream" });
createReadStream(file).pipe(response);
} catch {
response.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
response.end("Not found");
}
});
server.listen(5184, "127.0.0.1", () => console.log("http://127.0.0.1:5184/"));