Simple image host.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. var http = require("http");
  2. var https = require("https");
  3. var fs = require("fs");
  4. var Context = require("./lib/context.js");
  5. var conf = JSON.parse(fs.readFileSync("conf.json"));
  6. var endpoints = {
  7. "/": "index.html",
  8. "/404": "404.html",
  9. "/viewer": "viewer.node.js"
  10. }
  11. //Prepare files
  12. var errs = false;
  13. Object.keys(endpoints).forEach(function(i) {
  14. try {
  15. //The endpoint is a function if the file ends with .node.js
  16. if (/\.node\.js$/.test(endpoints[i])) {
  17. endpoints[i] = require("./"+conf.webroot+"/"+endpoints[i]);
  18. //If it doesn't end with .node.js, it's a regular text file and will
  19. //just be served as is
  20. } else {
  21. endpoints[i] = fs.readFileSync(conf.webroot+"/"+endpoints[i], "utf8");
  22. }
  23. //Errors will usually be because an endpoint doesn't exist
  24. } catch (err) {
  25. if (err.code == "ENOENT") {
  26. console.log(err.toString());
  27. errs = true;
  28. } else {
  29. throw err;
  30. }
  31. }
  32. });
  33. //No need to proceed if some endpoints don't exist
  34. if (errs) process.exit();
  35. //Prepare all templates
  36. var templates = {};
  37. fs.readdirSync("templates").forEach(function(f) {
  38. templates[f.replace(/\.html$/, "")] = fs.readFileSync("templates/"+f, "utf8");
  39. });
  40. function onRequest(req, res) {
  41. console.log("Request for "+req.url);
  42. var ep = endpoints[req.url];
  43. //If the file doesn't exist, we 404.
  44. if (!ep) {
  45. ep = endpoints["/404"];
  46. res.writeHead(404);
  47. }
  48. //Execute if it's a .node.js, or just respond with the contents of the file
  49. if (typeof ep == "function") {
  50. ep(new Context(req, res, templates, conf));
  51. } else {
  52. res.end(ep);
  53. }
  54. }
  55. var server;
  56. if (conf.use_https) {
  57. server = https.createServer(conf.https, onRequest);
  58. } else {
  59. server = http.createServer(onRequest);
  60. }
  61. server.listen(conf.port);
  62. console.log("Listening on port "+conf.port+".");