import fs from "node:fs"; import http from "node:http"; import path from "node:path"; import { fileURLToPath } from "node:url"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const root = path.resolve(__dirname, "../frontend"); const host = process.env.FRONTEND_HOST || "127.0.0.1"; const port = Number(process.env.FRONTEND_PORT || "5173"); const types = { ".html": "text/html; charset=utf-8", ".css": "text/css; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".svg": "image/svg+xml", ".png": "image/png", ".ico": "image/x-icon", ".xml": "application/xml; charset=utf-8", ".webmanifest": "application/manifest+json; charset=utf-8", ".txt": "text/plain; charset=utf-8", ".md": "text/markdown; charset=utf-8", }; const server = http.createServer((request, response) => { const url = new URL(request.url || "/", `http://${host}`); const requestedPath = url.pathname === "/" ? "/index.html" : url.pathname === "/how-it-works" || url.pathname === "/how-it-works/" ? "/how-it-works/index.html" : url.pathname; const filePath = path.resolve(root, `.${requestedPath}`); if (!filePath.startsWith(root)) { response.writeHead(403); response.end("forbidden"); return; } fs.readFile(filePath, (error, data) => { if (error) { response.writeHead(404); response.end("not found"); return; } response.writeHead(200, { "content-type": types[path.extname(filePath)] || "application/octet-stream", }); response.end(data); }); }); server.listen(port, host, () => { console.log(`frontend http://${host}:${port}`); });