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.

114 lines
3.1 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. const express = require("express");
  10. const session = require('express-session');
  11. const includes = require('./includes/includes.js');
  12. const utils = require('./utils/utils.js');
  13. const map = require('./utils/generateSiteMap.js');
  14. map.main();
  15. const app = express();
  16. app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }}));
  17. const port = 8000;
  18. const cache = require('memory-cache');
  19. /**
  20. * Parses the request url and calls correct JS files
  21. */
  22. app.use(function(request, res)
  23. {
  24. if(request.headers.host.includes("localhost:" + port) ||
  25. request.headers.host.includes("jrtechs.net"))
  26. {
  27. const filename = url.parse(request.url, true).pathname;
  28. //handles image requests
  29. if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png"))
  30. {
  31. require("./img/image.js").main(res, filename, cache);
  32. }
  33. else if(filename.includes("/css/") || filename.includes(".txt"))
  34. {
  35. includes.sendCSS(res, filename, cache)
  36. }
  37. else if(filename.includes("/js/") || filename.includes(".js"))
  38. {
  39. require("./js/js.js").main(res, filename, cache);
  40. }
  41. else if(filename.includes("/downloads/"))
  42. {
  43. require("./downloads/downloads.js").main(res, filename, request);
  44. }
  45. else
  46. {
  47. var file = "";
  48. var html = cache.get(filename);
  49. res.writeHead(200, {'Content-Type': 'text/html'});
  50. if(html == null)
  51. {
  52. if(filename === '' || filename === '/')
  53. {
  54. file="./posts/homePage.js";
  55. }
  56. else
  57. {
  58. var urlSplit = filename.split("/");
  59. if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
  60. file = "./posts/category.js";
  61. else if(urlSplit.length >= 2 && urlSplit[1] === 'admin') //top secret admin page
  62. file = "./admin/admin.js";
  63. else
  64. file = "./posts/posts.js";
  65. }
  66. Promise.all([includes.printHeader(),
  67. require(file).main(filename, request),
  68. includes.printFooter()]).then(function(content)
  69. {
  70. res.write(content.join(''));
  71. res.end();
  72. cache.put(filename, content.join(''));
  73. }).catch(function(err)
  74. {
  75. console.log(err);
  76. throw err;
  77. });
  78. }
  79. else
  80. {
  81. res.write(html);
  82. res.end();
  83. }
  84. }
  85. }
  86. else
  87. {
  88. utils.printWrongHost(res);
  89. res.end();
  90. }
  91. });
  92. http.createServer(app).listen(port);