105 lines
3.7 KiB
JavaScript
105 lines
3.7 KiB
JavaScript
const http = require("http");
|
||
const fs = require("fs");
|
||
const path = require("path");
|
||
|
||
const PORT = process.env.PORT || 3000;
|
||
const DATA = path.join(__dirname, "data.json");
|
||
|
||
function readData() {
|
||
try { return JSON.parse(fs.readFileSync(DATA, "utf8")); } catch (_) { return { events: [] }; }
|
||
}
|
||
function writeData(data) {
|
||
fs.writeFileSync(DATA, JSON.stringify(data, null, 2));
|
||
}
|
||
function body(req) {
|
||
return new Promise((resolve) => {
|
||
let s = "";
|
||
req.on("data", (c) => (s += c));
|
||
req.on("end", () => { try { resolve(JSON.parse(s || "{}")); } catch (_) { resolve({}); } });
|
||
});
|
||
}
|
||
|
||
http.createServer(async (req, res) => {
|
||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
|
||
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
||
|
||
if (req.method === "OPTIONS") {
|
||
res.writeHead(200);
|
||
return res.end();
|
||
}
|
||
|
||
const urlPath = req.url.split("?")[0];
|
||
const queryString = req.url.split("?")[1] || "";
|
||
const query = new URLSearchParams(queryString);
|
||
|
||
// API: GET /api/events
|
||
if (req.method === "GET" && urlPath === "/api/events") {
|
||
const tab = query.get("tab") || "biot";
|
||
const data = readData();
|
||
const events = data[tab + "Events"] || [];
|
||
res.writeHead(200, { "Content-Type": "application/json" });
|
||
return res.end(JSON.stringify(events));
|
||
}
|
||
|
||
// API: POST /api/events
|
||
if (req.method === "POST" && urlPath === "/api/events") {
|
||
const tab = query.get("tab") || "biot";
|
||
console.log("POST /api/events tab:", tab);
|
||
const data = readData();
|
||
const event = await body(req);
|
||
console.log("Event data:", event);
|
||
event.id = Date.now();
|
||
event.tab = tab;
|
||
const arrayName = tab + "Events";
|
||
if (!data[arrayName]) data[arrayName] = [];
|
||
data[arrayName].push(event);
|
||
console.log("Saving to:", arrayName, "Total:", data[arrayName].length);
|
||
writeData(data);
|
||
res.writeHead(200, { "Content-Type": "application/json" });
|
||
return res.end(JSON.stringify({ ok: true, event }));
|
||
}
|
||
|
||
// API: PUT /api/events/:id
|
||
if (req.method === "PUT" && urlPath.startsWith("/api/events/")) {
|
||
const tab = query.get("tab") || "biot";
|
||
const id = parseInt(urlPath.split("/")[3]);
|
||
const data = readData();
|
||
const events = data[tab + "Events"] || [];
|
||
const idx = events.findIndex(e => e.id === id);
|
||
if (idx !== -1) {
|
||
events[idx] = { ...events[idx], ...await body(req) };
|
||
data[tab + "Events"] = events;
|
||
writeData(data);
|
||
res.writeHead(200, { "Content-Type": "application/json" });
|
||
return res.end(JSON.stringify({ ok: true }));
|
||
}
|
||
res.writeHead(404);
|
||
return res.end(JSON.stringify({ error: "Не найдено" }));
|
||
}
|
||
|
||
// API: DELETE /api/events/:id
|
||
if (req.method === "DELETE" && urlPath.startsWith("/api/events/")) {
|
||
const tab = query.get("tab") || "biot";
|
||
const id = parseInt(urlPath.split("/")[3]);
|
||
const data = readData();
|
||
data[tab + "Events"] = (data[tab + "Events"] || []).filter(e => e.id !== id);
|
||
writeData(data);
|
||
res.writeHead(200, { "Content-Type": "application/json" });
|
||
return res.end(JSON.stringify({ ok: true }));
|
||
}
|
||
|
||
// Статика
|
||
let file = urlPath === "/" ? "/index.html" : urlPath;
|
||
const full = path.join(__dirname, file);
|
||
if (full.startsWith(__dirname) && fs.existsSync(full) && fs.statSync(full).isFile()) {
|
||
const ext = path.extname(full);
|
||
const type = ext === ".css" ? "text/css" : ext === ".js" ? "application/javascript" : "text/html";
|
||
res.writeHead(200, { "Content-Type": type + "; charset=utf-8" });
|
||
return res.end(fs.readFileSync(full));
|
||
}
|
||
|
||
res.writeHead(404);
|
||
res.end("Not found");
|
||
}).listen(PORT, () => console.log("Сервер запущен на порту " + PORT));
|