Simple image host.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

index.js 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. var http = require("http");
  2. var https = require("https");
  3. var fs = require("fs");
  4. var loader = require("./lib/loader.js");
  5. var Context = require("./lib/context.js");
  6. var Db = require("./lib/db.js");
  7. var conf = JSON.parse(fs.readFileSync("conf.json"));
  8. var endpoints = {
  9. //General files
  10. "/favicon.ico": "favicon.ico",
  11. "/global.css": "global.css",
  12. "/global.js": "global.js",
  13. "/404": "404.html",
  14. //Index files
  15. "/": "index/index.node.js",
  16. "/index/script.js": "index/script.js",
  17. "/index/style.css": "index/style.css",
  18. //Viewer files
  19. "/viewer": "viewer/index.node.js",
  20. "/viewer/script.js": "viewer/script.js",
  21. "/viewer/style.css": "viewer/style.css",
  22. //API files
  23. "/api/upload": "api/upload.node.js"
  24. }
  25. var loaded = loader.load(endpoints, conf);
  26. //Function to run on each request
  27. function onRequest(req, res) {
  28. console.log("Request for "+req.url);
  29. var ep = loaded.endpoints[req.url];
  30. //If the file doesn't exist, we 404.
  31. if (!ep) {
  32. ep = loaded.endpoints["/404"];
  33. res.writeHead(404);
  34. }
  35. //Execute if it's a .node.js, or just respond with the contents of the file
  36. if (typeof ep == "function") {
  37. ep(new Context({
  38. req: req,
  39. res: res,
  40. templates: loaded.templates,
  41. views: loaded.views,
  42. conf: conf
  43. }));
  44. } else {
  45. res.end(ep);
  46. }
  47. }
  48. //Initiate a postgresql client
  49. var db = new Db(conf.db, function(err) {
  50. if (err) throw err;
  51. //Create HTTP or HTTPS server
  52. var server;
  53. if (conf.use_https) {
  54. server = https.createServer(conf.https, onRequest);
  55. } else {
  56. server = http.createServer(onRequest);
  57. }
  58. server.listen(conf.port);
  59. console.log("Listening on port "+conf.port+".");
  60. });