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.

97 lines
2.9 KiB

  1. //used to append static content to result
  2. const includes = require('../includes/includes.js');
  3. //used to append static content to result
  4. const contentLoader = require('../includes/staticContentServer.js');
  5. //caching program to make the application run faster
  6. const cache = require('memory-cache');
  7. /**
  8. * @author Jeffery Russell 11-3-18
  9. *
  10. * @type {{main: (function(*=, *): Promise)}}
  11. */
  12. module.exports=
  13. {
  14. /**
  15. * Calls posts and sidebar modules to render blog contents in order
  16. *
  17. * @param requestURL
  18. * @returns {Promise|*}
  19. */
  20. main: function(request, result, filename)
  21. {
  22. if(contentLoader.serveStaticContent(request, result, filename, ""))
  23. {
  24. //do nothing
  25. }
  26. //downloads
  27. else if (filename.includes("/downloads/"))
  28. {
  29. require("../includes/downloads.js").main(result, filename);
  30. }
  31. else if (filename.includes("/contact"))
  32. {
  33. require("../includes/contact.js").main(request, result);
  34. }
  35. else
  36. {
  37. var page = request.query.page;
  38. if(typeof page == "undefined")
  39. page = 1;
  40. page = Number(page);
  41. const html = cache.get(filename + "?page=" + page);
  42. result.writeHead(200, {'Content-Type': 'text/html'});
  43. if (html == null) {
  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
  55. file = "../posts/posts.js";
  56. }
  57. Promise.all([includes.printHeader(),
  58. require(file).main(filename, request),
  59. includes.printFooter()]).then(function (content)
  60. {
  61. result.write(content.join(''));
  62. result.end();
  63. cache.put(filename + "?page=" + page, content.join(''));
  64. }).catch(function (err)
  65. {
  66. console.log(err);
  67. throw err;
  68. });
  69. }
  70. else
  71. {
  72. result.write(html);
  73. result.end();
  74. }
  75. }
  76. },
  77. /**
  78. * Clears the memory cache.
  79. */
  80. clearCache: function()
  81. {
  82. console.log("Blog cache cleared");
  83. cache.clear();
  84. }
  85. };