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.

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