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.

server.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. var fs = require("fs");
  2. var http = require("http");
  3. var crypto = require("crypto");
  4. var pathlib = require("path");
  5. var urllib = require("url");
  6. var Slideshow = require("./js/slideshow");
  7. var error = require("./js/error");
  8. var conf = JSON.parse(fs.readFileSync("conf.json"));
  9. var index = fs.readFileSync("index.html", "utf-8")
  10. .replace(/<<transition_time>>/g, conf.transition_time);
  11. var slideshow = Slideshow(conf.slides, conf.interval);
  12. function onexit(code) {
  13. console.log("exiting", code);
  14. slideshow.sendEvent("reload");
  15. process.exit();
  16. }
  17. process.on("exit", onexit);
  18. process.on("SIGINT", onexit);
  19. process.on("SIGTERM", onexit);
  20. process.on("uncaughtException", onexit);
  21. function handler(req, res) {
  22. var parts = urllib.parse(req.url);
  23. // /: Send the base site to the client
  24. if (parts.pathname === "/") {
  25. res.end(index);
  26. // /polyfills.js: JavaScript polyfills
  27. } else if (parts.pathname === "polyfills.js") {
  28. fs.createReadStream("polyfills.js")
  29. .pipe(res)
  30. .on("error", err => error(err, res));
  31. // /init: send initial information about current slide
  32. } else if (parts.pathname === "/init") {
  33. res.end(slideshow.getSlideName());
  34. // /await: long polling, request won't end before a new slide comes
  35. } else if (parts.pathname === "/await") {
  36. slideshow.pushAwaiter(res);
  37. // /slide: serve the current slide's html
  38. } else if (parts.pathname === "/slide") {
  39. slideshow.serveCurrentSlide(parts, res);
  40. // Serve other files
  41. } else {
  42. var served = false;
  43. for (var slide of slideshow.getSlides()) {
  44. // If client requests /{slide-name}/*
  45. if (slide.name === parts.pathname.substr(1, slide.name.length)) {
  46. slide.serveFiles(parts, res);
  47. served = true;
  48. break;
  49. }
  50. }
  51. if (!served) {
  52. res.writeHead(404);
  53. res.end("404");
  54. }
  55. }
  56. }
  57. var server = http.createServer(handler);
  58. server.on("error", err => {
  59. console.error(err);
  60. system.exit(1);
  61. });
  62. server.listen(conf.port);
  63. console.log("Server running on port "+conf.port+".");