From b051332fc0845659ea9f457c77f5d7952e7763c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Tue, 8 Jun 2021 18:36:05 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BE=D1=82=D1=81=D0=BB=D0=B5=D0=B6=D0=B8?= =?UTF-8?q?=D0=B2=D0=B0=D1=82=D1=8C=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20json.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- сервер.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 сервер.js diff --git a/сервер.js b/сервер.js new file mode 100644 index 0000000..793a447 --- /dev/null +++ b/сервер.js @@ -0,0 +1,62 @@ +const fs = require("fs"); +const http = require("http"); +const path = require("path"); + +var jsonUpdates = {}; + +const srv = http.createServer(process); +srv.listen(8000); +fs.watch(__dirname, trackJSONUpdates); + +function process(req, res) +{ + var fileName = "." + decodeURI(req.url); + if (fileName == "./") + { + fileName = "./index.html"; + } + else if (fileName == "./json.js") + { + const content = Object.keys(jsonUpdates).sort().join("\n"); + jsonUpdates = {}; + res.writeHead(200); + res.end(content, "utf-8"); + return; + } +console.debug("fileName:", fileName); + const ext = path.extname(fileName); + var contentType = "text/html"; + switch (ext) + { + case ".js": + contentType = "text/javascript"; + break; + case "png": + contentType = "image/png"; + break; + case "jpg": + contentType = "image/jpg"; + break; + } + fs.readFile(fileName, function(err, content) { + if (err) + { + res.writeHead(500); + res.end(err.code); + return; + } + res.writeHead(200, { "Content-Type": contentType }); + res.end(content, "utf-8"); + }); +} + +function trackJSONUpdates(eventName, fileName) +{ + if (!fileName.endsWith("json.js")) + { + return; + } + jsonUpdates[fileName] = true; +console.debug("fileName", fileName); +} +