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.

130 lines
3.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. 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(".woff2"))
  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 if(filename.includes("/admin"))
  46. {
  47. res.writeHead(200, {'Content-Type': 'text/html'});
  48. file = "./admin/admin.js";
  49. Promise.all([includes.printHeader(),
  50. require(file).main(filename, request),
  51. includes.printFooter()]).then(function(content)
  52. {
  53. res.write(content.join(''));
  54. res.end();
  55. }).catch(function(err)
  56. {
  57. console.log(err);
  58. throw err;
  59. });
  60. }
  61. else
  62. {
  63. var file = "";
  64. var html = cache.get(filename);
  65. res.writeHead(200, {'Content-Type': 'text/html'});
  66. if(html == null)
  67. {
  68. if(filename === '' || filename === '/')
  69. {
  70. file="./posts/homePage.js";
  71. }
  72. else
  73. {
  74. var urlSplit = filename.split("/");
  75. if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
  76. file = "./posts/category.js";
  77. else
  78. file = "./posts/posts.js";
  79. }
  80. Promise.all([includes.printHeader(),
  81. require(file).main(filename, request),
  82. includes.printFooter()]).then(function(content)
  83. {
  84. res.write(content.join(''));
  85. res.end();
  86. cache.put(filename, content.join(''));
  87. }).catch(function(err)
  88. {
  89. console.log(err);
  90. throw err;
  91. });
  92. }
  93. else
  94. {
  95. res.write(html);
  96. res.end();
  97. }
  98. }
  99. }
  100. else
  101. {
  102. utils.printWrongHost(res);
  103. res.end();
  104. }
  105. });
  106. http.createServer(app).listen(port);