serve.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. Minimal static server for the ArozOS web root, so the Cypress suite can
  3. run without Python or any other tooling. Serves ../../../src/web on
  4. WEB_PORT (default 8123).
  5. */
  6. "use strict";
  7. const http = require("http");
  8. const fs = require("fs");
  9. const path = require("path");
  10. const ROOT = path.resolve(__dirname, "..", "..", "..", "src", "web");
  11. const PORT = parseInt(process.env.WEB_PORT || "8123", 10);
  12. const TYPES = {
  13. ".html": "text/html; charset=utf-8",
  14. ".js": "text/javascript; charset=utf-8",
  15. ".css": "text/css; charset=utf-8",
  16. ".json": "application/json",
  17. ".png": "image/png",
  18. ".jpg": "image/jpeg",
  19. ".svg": "image/svg+xml",
  20. ".woff": "font/woff",
  21. ".woff2": "font/woff2",
  22. ".ico": "image/x-icon"
  23. };
  24. http.createServer(function (req, res) {
  25. const urlPath = decodeURIComponent(req.url.split("?")[0]);
  26. let file = path.normalize(path.join(ROOT, urlPath));
  27. if (!file.startsWith(ROOT)) {
  28. res.writeHead(403);
  29. res.end();
  30. return;
  31. }
  32. fs.stat(file, function (err, st) {
  33. if (!err && st.isDirectory()) { file = path.join(file, "index.html"); }
  34. fs.readFile(file, function (err2, data) {
  35. if (err2) {
  36. res.writeHead(404);
  37. res.end("not found");
  38. return;
  39. }
  40. res.writeHead(200, { "Content-Type": TYPES[path.extname(file).toLowerCase()] || "application/octet-stream" });
  41. res.end(data);
  42. });
  43. });
  44. }).listen(PORT, "127.0.0.1", function () {
  45. console.log("Serving " + ROOT + " on http://127.0.0.1:" + PORT);
  46. });