Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
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.

100 lines
2.5 KiB

  1. /**
  2. * Main server file for the blog. This file is responsible for
  3. * creating the server and listening for clients. The main run
  4. * function parses the url and calls a sub module to make the
  5. * appropriate pages.
  6. */
  7. const http = require('http');
  8. const url = require('url');
  9. var express = require("express");
  10. var session = require('express-session');
  11. const includes = require('./includes/includes.js');
  12. const utils = require('./utils/utils.js');
  13. var map = require('./utils/generateSiteMap.js');
  14. map.main();
  15. var app = express();
  16. app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }}));
  17. var port = 8000;
  18. /**
  19. * Parses the request url and calls correct JS files
  20. */
  21. app.use(function(request, res)
  22. {
  23. console.log(request.headers.host);
  24. if(request.headers.host.includes("localhost:" + port) ||
  25. request.headers.host.includes("jrtechs.net"))
  26. {
  27. var q = url.parse(request.url, true);
  28. var filename = q.pathname;
  29. //handles image requests
  30. if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png"))
  31. {
  32. require("./img/image.js").main(res, filename);
  33. }
  34. else if(filename.includes("/css/") || filename.includes(".txt"))
  35. {
  36. includes.sendCSS(res, filename)
  37. }
  38. else if(filename.includes("/downloads/"))
  39. {
  40. require("./downloads/downloads.js").main(res, filename, request);
  41. }
  42. else
  43. {
  44. var file = "";
  45. if(filename === '' || filename === '/')
  46. {
  47. file="./posts/homePage.js";
  48. }
  49. else
  50. {
  51. var urlSplit = filename.split("/");
  52. if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
  53. file = "./posts/category.js";
  54. else if(urlSplit.length >= 2 && urlSplit[1] === 'admin') //top secret admin page
  55. file = "./admin/admin.js";
  56. else
  57. file = "./posts/posts.js";
  58. }
  59. includes.printHeader(res).then(function()
  60. {
  61. return require(file).main(res, filename, request);
  62. }).then(function()
  63. {
  64. return includes.printFooter(res);
  65. }).catch(function(err)
  66. {
  67. console.log(err);
  68. })
  69. }
  70. }
  71. else
  72. {
  73. utils.printWrongHost(res);
  74. res.end();
  75. }
  76. });
  77. http.createServer(app).listen(port);