Проверить Matter.js
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

сервер.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const fs = require("fs");
  2. const http = require("http");
  3. const path = require("path");
  4. var jsonUpdates = {};
  5. const srv = http.createServer(process);
  6. srv.listen(8000);
  7. fs.watch(__dirname, trackJSONUpdates);
  8. function process(req, res)
  9. {
  10. var fileName = "." + decodeURI(req.url);
  11. if (fileName == "./")
  12. {
  13. fileName = "./index.html";
  14. }
  15. else if (fileName == "./json.js")
  16. {
  17. const content = Object.keys(jsonUpdates).sort().join("\n");
  18. jsonUpdates = {};
  19. res.writeHead(200, { "Content-Type": "text/javascript" });
  20. res.end(content, "utf-8");
  21. return;
  22. }
  23. console.debug("fileName:", fileName);
  24. const ext = path.extname(fileName);
  25. var contentType = "text/html";
  26. switch (ext)
  27. {
  28. case ".js":
  29. contentType = "text/javascript";
  30. break;
  31. case "png":
  32. contentType = "image/png";
  33. break;
  34. case "jpg":
  35. contentType = "image/jpg";
  36. break;
  37. case "wav":
  38. contentType = "image/vnd.wave";
  39. break;
  40. }
  41. fs.readFile(fileName, function(err, content) {
  42. if (err)
  43. {
  44. res.writeHead(500);
  45. res.end(err.code);
  46. return;
  47. }
  48. res.writeHead(200, { "Content-Type": contentType });
  49. res.end(content, "utf-8");
  50. });
  51. }
  52. function trackJSONUpdates(eventName, fileName)
  53. {
  54. if (!fileName.endsWith("json.js"))
  55. {
  56. return;
  57. }
  58. jsonUpdates[fileName] = true;
  59. console.debug("fileName", fileName);
  60. }