76 lines
3.0 KiB
JavaScript
76 lines
3.0 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 { producers: [], products: [] }; }
|
||
}
|
||
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) => {
|
||
// API: получить все данные
|
||
if (req.method === "GET" && req.url === "/api/data") {
|
||
res.writeHead(200, { "Content-Type": "application/json" });
|
||
return res.end(JSON.stringify(readData()));
|
||
}
|
||
// API: добавить производителя
|
||
if (req.method === "POST" && req.url === "/api/producers") {
|
||
const p = await body(req);
|
||
const data = readData();
|
||
data.producers.push({ id: Date.now(), ...p });
|
||
writeData(data);
|
||
res.writeHead(200, { "Content-Type": "application/json" });
|
||
return res.end(JSON.stringify({ ok: true }));
|
||
}
|
||
// API: добавить товар с ценой
|
||
if (req.method === "POST" && req.url === "/api/products") {
|
||
const prod = await body(req);
|
||
const data = readData();
|
||
data.products.push({ id: Date.now(), ...prod });
|
||
writeData(data);
|
||
res.writeHead(200, { "Content-Type": "application/json" });
|
||
return res.end(JSON.stringify({ ok: true }));
|
||
}
|
||
// API: удалить производителя
|
||
if (req.method === "DELETE" && req.url.startsWith("/api/producers/")) {
|
||
const id = parseInt(req.url.split("/")[3]);
|
||
const data = readData();
|
||
data.producers = data.producers.filter(p => p.id !== id);
|
||
data.products = data.products.filter(pr => pr.producerId !== id);
|
||
writeData(data);
|
||
res.writeHead(200, { "Content-Type": "application/json" });
|
||
return res.end(JSON.stringify({ ok: true }));
|
||
}
|
||
// API: удалить товар
|
||
if (req.method === "DELETE" && req.url.startsWith("/api/products/")) {
|
||
const id = parseInt(req.url.split("/")[3]);
|
||
const data = readData();
|
||
data.products = data.products.filter(p => p.id !== id);
|
||
writeData(data);
|
||
res.writeHead(200, { "Content-Type": "application/json" });
|
||
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));
|