const http = require("node:http"); const fs = require("node:fs"); const path = require("node:path"); const PORT = Number.parseInt(process.env.PORT ?? "8080", 10) || 8080; function sendText(res, statusCode, text) { res.writeHead(statusCode, { "Content-Type": "text/plain; charset=utf-8" }); res.end(text); } function sendFile(res, filePath, contentType) { fs.readFile(filePath, (err, data) => { if (err) { sendText(res, 500, "Internal Server Error"); return; } res.writeHead(200, { "Content-Type": contentType, "Cache-Control": "no-store", }); res.end(data); }); } const server = http.createServer((req, res) => { if (req.method !== "GET") { sendText(res, 405, "Method Not Allowed"); return; } const url = req.url ?? "/"; if (url === "/" || url === "/index.html") { sendFile( res, path.join(__dirname, "index.html"), "text/html; charset=utf-8", ); return; } if (url === "/handler.js") { sendFile( res, path.join(__dirname, "handler.js"), "text/javascript; charset=utf-8", ); return; } sendText(res, 404, "Not Found"); }); server.listen(PORT, "0.0.0.0", () => { // eslint-disable-next-line no-console console.log(`Server running on http://0.0.0.0:${PORT}`); });