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.

107 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. 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. var page = request.query.page;
  29. if(typeof page == "undefined")
  30. page = 1;
  31. page = Number(page);
  32. const html = cache.get(filename + "?page=" + page);
  33. result.writeHead(200, {'Content-Type': 'text/html'});
  34. if (html == null)
  35. {
  36. var file = "";
  37. if (filename === '' || filename === '/')
  38. {
  39. file = "../blog/homePage.js";
  40. }
  41. else
  42. {
  43. var urlSplit = filename.split("/");
  44. if (urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
  45. file = "../blog/category.js";
  46. else
  47. {
  48. file = "../blog/posts.js";
  49. page = 1; // all blog are single page, everyone must be one to ensure
  50. // cache is not tricked into storing same blog post a ton of times
  51. }
  52. }
  53. var templateContext = Object();
  54. Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
  55. utils.includeInObject(PAGINATION_TEMPLATE_KEY, templateContext, "templates/" + PAGINATION_TEMPLATE_FILE),
  56. includes.printHeader(templateContext),
  57. includes.printFooter(templateContext),
  58. require(file).main(filename, request, templateContext),
  59. require("../blog/sidebar.js").main(templateContext)])
  60. .then(function (content)
  61. {
  62. const html = whiskers.render(content[0], templateContext);
  63. result.write(html);
  64. result.end();
  65. cache.put(filename + "?page=" + page, html);
  66. }).catch(function (err)
  67. {
  68. console.log(err);
  69. cache.del(filename + "?page=" + page);
  70. utils.print404().then(function(content)
  71. {
  72. result.write(content);
  73. result.end();
  74. })
  75. });
  76. }
  77. else
  78. {
  79. result.write(html);
  80. result.end();
  81. }
  82. },
  83. /**
  84. * Clears the memory cache.
  85. */
  86. clearCache: function()
  87. {
  88. console.log("Blog cache cleared");
  89. cache.clear();
  90. }
  91. };