privet-mozhesh-mne-sozdat-fo/server.js

76 lines
3.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 readItems() {
try { return JSON.parse(fs.readFileSync(DATA, "utf8")); } catch (_) { return []; }
}
function writeItems(items) {
fs.writeFileSync(DATA, JSON.stringify(items, 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({}); } });
});
}
const TYPES = {
".html": "text/html", ".css": "text/css", ".js": "application/javascript",
".json": "application/json", ".svg": "image/svg+xml", ".png": "image/png",
".ico": "image/x-icon", ".woff2": "font/woff2"
};
http.createServer(async (req, res) => {
const url = req.url.split("?")[0];
if (req.method === "GET" && url === "/api/registrations") {
res.writeHead(200, { "Content-Type": "application/json" });
return res.end(JSON.stringify(readItems()));
}
if (req.method === "GET" && url === "/api/status") {
const q = new URL(req.url, "http://x").searchParams;
const email = (q.get("email") || "").trim().toLowerCase();
const found = readItems().find(i => i.email.toLowerCase() === email);
res.writeHead(200, { "Content-Type": "application/json" });
return res.end(JSON.stringify({ ok: true, registration: found || null }));
}
if (req.method === "POST" && url === "/api/register") {
const item = await body(req);
if (!item.fio || !item.email || !item.phone || !item.city || !item.track || !item.role || !item.experience) {
res.writeHead(400, { "Content-Type": "application/json" });
return res.end(JSON.stringify({ ok: false, error: "Заполнены не все обязательные поля" }));
}
const items = readItems();
if (items.some(i => i.email.toLowerCase() === item.email.toLowerCase())) {
res.writeHead(409, { "Content-Type": "application/json" });
return res.end(JSON.stringify({ ok: false, error: "На этот email заявка уже отправлена" }));
}
items.push({
id: Date.now(),
fio: item.fio, email: item.email, phone: item.phone, city: item.city,
track: item.track, role: item.role, team: item.team || "",
experience: item.experience, createdAt: new Date().toISOString()
});
writeItems(items);
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).toLowerCase();
const type = TYPES[ext] || "application/octet-stream";
res.writeHead(200, { "Content-Type": type + (ext === ".woff2" ? "" : "; charset=utf-8") });
return res.end(fs.readFileSync(full));
}
res.writeHead(404); res.end("Not found");
}).listen(PORT, () => console.log("Сервер на порту " + PORT));