90 lines
2.8 KiB
JavaScript
90 lines
2.8 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");
|
|
const EMPTY = {
|
|
child: { name: "Тимур", age: "2 года", note: "" },
|
|
words: [],
|
|
phrases: [],
|
|
tasks: [],
|
|
goals: [],
|
|
journal: [],
|
|
comprehension: [],
|
|
activity: [],
|
|
articulation: [],
|
|
games: []
|
|
};
|
|
|
|
function load() {
|
|
try { return { ...EMPTY, ...JSON.parse(fs.readFileSync(DATA, "utf8")) }; }
|
|
catch (_) { return { ...EMPTY }; }
|
|
}
|
|
|
|
function save(d) {
|
|
fs.writeFileSync(DATA, JSON.stringify(d, 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({}); }
|
|
});
|
|
});
|
|
}
|
|
|
|
function send(res, data, status = 200) {
|
|
res.writeHead(status, { "Content-Type": "application/json; charset=utf-8" });
|
|
res.end(JSON.stringify(data));
|
|
}
|
|
|
|
const server = http.createServer(async (req, res) => {
|
|
const url = req.url.split("?")[0];
|
|
|
|
if (req.method === "GET" && url === "/api/data") {
|
|
return send(res, load());
|
|
}
|
|
|
|
if (req.method === "POST" && url === "/api/data") {
|
|
const payload = await body(req);
|
|
const data = load();
|
|
if (payload.kind === "sync") {
|
|
data.child = payload.child || data.child;
|
|
data.words = payload.words || data.words;
|
|
data.phrases = payload.phrases || data.phrases;
|
|
data.tasks = payload.tasks || data.tasks;
|
|
data.goals = payload.goals || data.goals;
|
|
data.journal = payload.journal || data.journal;
|
|
data.comprehension = payload.comprehension || data.comprehension;
|
|
data.activity = payload.activity || data.activity;
|
|
data.articulation = payload.articulation || data.articulation;
|
|
data.games = payload.games || data.games;
|
|
} else {
|
|
for (const key of ["child", "words", "phrases", "tasks", "goals", "journal", "comprehension", "activity", "articulation", "games"]) {
|
|
if (payload[key] !== undefined) data[key] = payload[key];
|
|
}
|
|
}
|
|
save(data);
|
|
return send(res, { ok: true, data });
|
|
}
|
|
|
|
let file = req.url === "/" ? "/index.html" : url;
|
|
const full = path.join(__dirname, file);
|
|
if (!full.startsWith(__dirname) || !fs.existsSync(full) || !fs.statSync(full).isFile()) {
|
|
res.writeHead(404);
|
|
return res.end("Not found");
|
|
}
|
|
const ext = path.extname(full).toLowerCase();
|
|
const headers = {
|
|
"Content-Type": (ext === ".css" ? "text/css" : ext === ".js" ? "application/javascript" : "text/html") + "; charset=utf-8"
|
|
};
|
|
if (file.startsWith("/design-system/icons/") || file.endsWith("icons.svg")) headers["Content-Type"] = "image/svg+xml";
|
|
res.writeHead(200, headers);
|
|
res.end(fs.readFileSync(full));
|
|
});
|
|
|
|
server.listen(PORT, () => console.log("Speech development cabinet running on " + PORT));
|