shablony-prodavanam/src/server.js
2026-11-15 16:31:30 +05:00

32 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { createServer } from "node:http";
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { renderLanding } from "./landing.js";
import { legalPage } from "./legal.js";
const send = (res, code, type, body) => {
res.writeHead(code, { "content-type": `${type}; charset=utf-8` });
res.end(body);
};
export function createApp(config) {
return createServer(async (req, res) => {
const path = new URL(req.url, "http://x").pathname;
try {
if (path === "/healthz") return send(res, 200, "text/plain", "ok");
if (path === "/") return send(res, 200, "text/html", renderLanding(config, "ru"));
if (path === "/en") return send(res, 200, "text/html", renderLanding(config, "en"));
const img = /^\/img\/([\w-]+\.svg)$/.exec(path);
if (img) return send(res, 200, "image/svg+xml", await readFile(join(config.publicDir, "img", img[1])));
const doc = /^\/(offer|refund)$/.exec(path);
if (doc) {
const html = await legalPage(config, doc[1]);
if (html) return send(res, 200, "text/html", html);
}
} catch (e) {
if (e.code !== "ENOENT") console.error(e);
}
send(res, 404, "text/plain", "Не найдено");
});
}