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.

117 lines
3.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. 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
  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. {
  47. var file = "";
  48. if (filename === '' || filename === '/')
  49. {
  50. file = "../blog/homePage.js";
  51. }
  52. else
  53. {
  54. var urlSplit = filename.split("/");
  55. if (urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
  56. file = "../blog/category.js";
  57. else
  58. {
  59. file = "../blog/posts.js";
  60. page = 1; // all blog are single page, everyone must be one to ensure
  61. // cache is not tricked into storing same blog post a ton of times
  62. }
  63. }
  64. var templateContext = Object();
  65. Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
  66. utils.includeInObject(PAGINATION_TEMPLATE_KEY, templateContext, "templates/" + PAGINATION_TEMPLATE_FILE),
  67. includes.printHeader(templateContext),
  68. includes.printFooter(templateContext),
  69. require(file).main(filename, request, templateContext),
  70. require("../blog/sidebar.js").main(templateContext)])
  71. .then(function (content)
  72. {
  73. const html = whiskers.render(content[0], templateContext);
  74. result.write(html);
  75. result.end();
  76. cache.put(filename + "?page=" + page, html);
  77. }).catch(function (err)
  78. {
  79. console.log(err);
  80. cache.del(filename + "?page=" + page);
  81. utils.print404().then(function(content)
  82. {
  83. result.write(content);
  84. result.end();
  85. })
  86. });
  87. }
  88. else
  89. {
  90. result.write(html);
  91. result.end();
  92. }
  93. }
  94. },
  95. /**
  96. * Clears the memory cache.
  97. */
  98. clearCache: function()
  99. {
  100. console.log("Blog cache cleared");
  101. cache.clear();
  102. }
  103. };