Simple image host.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

index.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. }
  23. var loaded = loader.load(endpoints, conf);
  24. //Function to run on each request
  25. function onRequest(req, res) {
  26. console.log("Request for "+req.url);
  27. var ep = loaded.endpoints[req.url];
  28. //If the file doesn't exist, we 404.
  29. if (!ep) {
  30. ep = loaded.endpoints["/404"];
  31. res.writeHead(404);
  32. }
  33. //Execute if it's a .node.js, or just respond with the contents of the file
  34. if (typeof ep == "function") {
  35. ep(new Context({
  36. req: req,
  37. res: res,
  38. templates: loaded.templates,
  39. views: loaded.views,
  40. conf: conf
  41. }));
  42. } else {
  43. res.end(ep);
  44. }
  45. }
  46. //Initiate a postgresql client
  47. var db = new Db(conf.db, function(err) {
  48. if (err) throw err;
  49. //Create HTTP or HTTPS server
  50. var server;
  51. if (conf.use_https) {
  52. server = https.createServer(conf.https, onRequest);
  53. } else {
  54. server = http.createServer(onRequest);
  55. }
  56. server.listen(conf.port);
  57. console.log("Listening on port "+conf.port+".");
  58. });