Pictures!
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.

fileserver.js 843B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var fs = require("fs");
  2. var pathlib = require("path");
  3. var webroot = "web";
  4. var files = {};
  5. function file(wpath, fpath) {
  6. if (typeof fpath === "object")
  7. return files[wpath] = fpath;
  8. if (fpath === undefined)
  9. fpath = wpath;
  10. files[wpath] = fs.readFileSync(pathlib.join(webroot, fpath));
  11. }
  12. file("/polyfills.js");
  13. file("/script.js");
  14. file("/slide.css");
  15. file("/slide.js");
  16. file("/admin/", "/admin/index.html");
  17. file("/admin/style.css");
  18. file("/admin/lib.js");
  19. file("/admin/view.js");
  20. exports.canServe = canServe;
  21. function canServe(parts) {
  22. return files[parts.pathname] || files[parts.pathname + "/"];
  23. }
  24. exports.serve = serve;
  25. function serve(parts, res) {
  26. var f = files[parts.pathname];
  27. if (!f && files[parts.pathname + "/"]) {
  28. res.writeHead(302, {
  29. location: parts.pathname+"/"
  30. });
  31. res.end();
  32. } else {
  33. res.end(f);
  34. }
  35. }