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.

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