const http = require("http"); const fs = require("fs"); const path = require("path"); const PORT = process.env.PORT; const DATA = path.join(__dirname, "data.json"); function readData() { try { return JSON.parse(fs.readFileSync(DATA, "utf8")); } catch (_) { return { classes: [], reminders: [] }; } } 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 url = req.url.split("?")[0]; // API: получить все кружки if (req.method === "GET" && url === "/api/classes") { const data = readData(); res.writeHead(200, { "Content-Type": "application/json" }); return res.end(JSON.stringify(data.classes || [])); } // API: добавить кружок if (req.method === "POST" && url === "/api/classes") { const item = await body(req); const data = readData(); if (!data.classes) data.classes = []; data.classes.push({ id: Date.now(), ...item }); writeData(data); res.writeHead(200, { "Content-Type": "application/json" }); return res.end(JSON.stringify({ ok: true })); } // API: обновить кружок if (req.method === "PUT" && url === "/api/classes") { const item = await body(req); const data = readData(); const idx = (data.classes || []).findIndex(c => c.id === item.id); if (idx >= 0) { data.classes[idx] = { ...data.classes[idx], ...item }; writeData(data); } res.writeHead(200, { "Content-Type": "application/json" }); return res.end(JSON.stringify({ ok: true })); } // API: удалить кружок if (req.method === "DELETE" && url === "/api/classes") { const { id } = await body(req); const data = readData(); data.classes = (data.classes || []).filter(c => c.id !== id); writeData(data); res.writeHead(200, { "Content-Type": "application/json" }); return res.end(JSON.stringify({ ok: true })); } // API: получить напоминания if (req.method === "GET" && url === "/api/reminders") { const data = readData(); res.writeHead(200, { "Content-Type": "application/json" }); return res.end(JSON.stringify(data.reminders || [])); } // API: добавить напоминание if (req.method === "POST" && url === "/api/reminders") { const item = await body(req); const data = readData(); if (!data.reminders) data.reminders = []; data.reminders.push({ id: Date.now(), ...item }); writeData(data); res.writeHead(200, { "Content-Type": "application/json" }); return res.end(JSON.stringify({ ok: true })); } // API: удалить напоминание if (req.method === "DELETE" && url === "/api/reminders") { const { id } = await body(req); const data = readData(); data.reminders = (data.reminders || []).filter(r => r.id !== id); writeData(data); res.writeHead(200, { "Content-Type": "application/json" }); return res.end(JSON.stringify({ ok: true })); } // Статика let file = url === "/" ? "/index.html" : url; 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));