sozday-analiz-rashodov-s-uka/server.js

37 lines
1.3 KiB
JavaScript

// Временная точка проверки: валидация script.js + data.json при старте.
try {
require("./script.js");
} catch (e) {
console.error("SYNTAX-FAIL script.js: " + e.message);
process.exit(1);
}
try {
const d = require("./data.json");
if (!Array.isArray(d.rows) || !d.rows.length) throw new Error("data.json: нет rows");
const n = d.rows.length;
const t25 = d.rows.reduce((a, r) => a + (r.y2025 || 0), 0);
console.log("CHECK: syntax OK, data.json rows: " + n + ", 2025 total: " + Math.round(t25).toLocaleString("ru-RU"));
} catch (e) {
console.error("DATA-FAIL: " + e.message);
process.exit(1);
}
const http = require("http");
const fs = require("fs");
const path = require("path");
const srv = http.createServer((req, res) => {
const url = req.url.split("?")[0];
if (url === "/" || url === "/index.html") {
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
return fs.createReadStream(path.join(__dirname, "index.html")).pipe(res);
}
if (url === "/data.json") {
res.writeHead(200, { "Content-Type": "application/json" });
return fs.createReadStream(path.join(__dirname, "data.json")).pipe(res);
}
res.writeHead(404);
res.end("not found");
});
srv.listen(process.env.PORT || 3000, () => {
console.log("server on port " + (process.env.PORT || 3000));
});