what-to-cook/src/server.js
Peppa 164c5e00cb
All checks were successful
peppa/lint lint: success за 1с
peppa/test test: success за 0с
peppa/build build: success за 0с
ci / checks (pull_request) Successful in 4s
deploy / deploy (pull_request) Successful in 13s
WTC-1: страница «Что приготовить сегодня»
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 05:49:18 +05:00

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);