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.

105 lines
3.4 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. //file io
  8. const utils = require('../utils/utils.js');
  9. /**
  10. * @author Jeffery Russell 11-3-18
  11. *
  12. * @type {{main: (function(*=, *): Promise)}}
  13. */
  14. module.exports=
  15. {
  16. /**
  17. * Calls posts and sidebar modules to render blog contents in order
  18. *
  19. * @param requestURL
  20. * @returns {Promise|*}
  21. */
  22. main: function(request, result, filename)
  23. {
  24. if(contentLoader.serveStaticContent(request, result, filename, ""))
  25. {
  26. //do nothing
  27. }
  28. //downloads
  29. else if (filename.includes("/downloads/"))
  30. {
  31. require("../includes/downloads.js").main(result, filename);
  32. }
  33. else if (filename.includes("/contact"))
  34. {
  35. require("../includes/contact.js").main(request, result);
  36. }
  37. else
  38. {
  39. var page = request.query.page;
  40. if(typeof page == "undefined")
  41. page = 1;
  42. page = Number(page);
  43. const html = cache.get(filename + "?page=" + page);
  44. result.writeHead(200, {'Content-Type': 'text/html'});
  45. if (html == null) {
  46. var file = "";
  47. if (filename === '' || filename === '/')
  48. {
  49. file = "../posts/homePage.js";
  50. }
  51. else
  52. {
  53. var urlSplit = filename.split("/");
  54. if (urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
  55. file = "../posts/category.js";
  56. else
  57. {
  58. file = "../posts/posts.js";
  59. page = 1; // all posts are single page, everyone must be one to ensure
  60. // cache is not tricked into storing same blog post a ton of times
  61. }
  62. }
  63. Promise.all([includes.printHeader(),
  64. require(file).main(filename, request),
  65. includes.printFooter()]).then(function (content)
  66. {
  67. result.write(content.join(''));
  68. result.end();
  69. cache.put(filename + "?page=" + page, content.join(''));
  70. }).catch(function (err)
  71. {
  72. cache.del(filename + "?page=" + page);
  73. utils.print404().then(function(content)
  74. {
  75. result.write(content);
  76. result.end();
  77. })
  78. });
  79. }
  80. else
  81. {
  82. result.write(html);
  83. result.end();
  84. }
  85. }
  86. },
  87. /**
  88. * Clears the memory cache.
  89. */
  90. clearCache: function()
  91. {
  92. console.log("Blog cache cleared");
  93. cache.clear();
  94. }
  95. };