Browse Source

отслеживать изменение json.js

master
parent
commit
b051332fc0
1 changed files with 62 additions and 0 deletions
  1. +62
    -0
      сервер.js

+ 62
- 0
сервер.js View File

@@ -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);
}


Loading…
Cancel
Save