93 lines
4.1 KiB
JavaScript
93 lines
4.1 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 EMPTY; }
|
|
}
|
|
function writeData(data) {
|
|
fs.writeFileSync(DATA, JSON.stringify(data, null, 2));
|
|
}
|
|
|
|
const EMPTY = {
|
|
amal: {
|
|
school: [
|
|
{ day: "Понедельник", items: ["Математика", "Русский язык", "Литература", "Физкультура"] },
|
|
{ day: "Вторник", items: ["Математика", "Казахский язык", "История", "Английский"] },
|
|
{ day: "Среда", items: ["Русский язык", "Литература", "Биология", "Физкультура"] },
|
|
{ day: "Четверг", items: ["Математика", "История", "География", "Труд"] },
|
|
{ day: "Пятница", items: ["Русский язык", "Английский", "ИЗО", "Классный час"] }
|
|
],
|
|
clubs: [
|
|
{ day: "Понедельник", name: "Балет", time: "17:00" },
|
|
{ day: "Среда", name: "Балет", time: "17:00" },
|
|
{ day: "Пятница", name: "Домбра", time: "16:00" }
|
|
]
|
|
},
|
|
amre: {
|
|
school: [
|
|
{ day: "Понедельник", items: ["Математика", "Русский язык", "Литература", "Физкультура"] },
|
|
{ day: "Вторник", items: ["Математика", "Казахский язык", "История", "Английский"] },
|
|
{ day: "Среда", items: ["Русский язык", "Литература", "Биология", "Физкультура"] },
|
|
{ day: "Четверг", items: ["Математика", "История", "География", "Труд"] },
|
|
{ day: "Пятница", items: ["Русский язык", "Английский", "ИЗО", "Классный час"] }
|
|
],
|
|
clubs: [
|
|
{ day: "Вторник", name: "Шахматы", time: "17:00" },
|
|
{ day: "Четверг", name: "Шахматы", time: "17:00" },
|
|
{ day: "Пятница", name: "Домбра", time: "16:00" }
|
|
]
|
|
},
|
|
items: {
|
|
school: ["Дневник", "Тетради", "Ручки", "Карандаши", "Линейка", "Ластик", "Пенал"],
|
|
ballet: ["Купальник", "Колготки", "Чешки", "Пачка", "Заколки", "Вода"],
|
|
chess: ["Шахматные часы", "Блокнот", "Ручка"],
|
|
dombra: ["Домбра", "Ноты", "Медиатор"]
|
|
}
|
|
};
|
|
|
|
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, OPTIONS");
|
|
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
|
|
|
if (req.method === "OPTIONS") {
|
|
res.writeHead(200);
|
|
return res.end();
|
|
}
|
|
|
|
if (req.method === "GET" && req.url === "/api/data") {
|
|
res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
|
|
return res.end(JSON.stringify(readData()));
|
|
}
|
|
|
|
if (req.method === "POST" && req.url === "/api/data") {
|
|
const data = await body(req);
|
|
writeData(data);
|
|
res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
|
|
return res.end(JSON.stringify({ ok: true }));
|
|
}
|
|
|
|
let file = req.url === "/" ? "/index.html" : req.url.split("?")[0];
|
|
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));
|