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.

82 lines
2.5 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. /**
  8. * @author Jeffery Russell 11-3-18
  9. *
  10. * @type {{main: (function(*=, *): Promise)}}
  11. */
  12. module.exports=
  13. {
  14. /**
  15. * Calls posts and sidebar modules to render blog contents in order
  16. *
  17. * @param requestURL
  18. * @returns {Promise|*}
  19. */
  20. main: function(request, result, filename)
  21. {
  22. if(contentLoader.serveStaticContent(request, result, filename, ""))
  23. {
  24. //do nothing
  25. }
  26. //downloads
  27. else if (filename.includes("/downloads/"))
  28. {
  29. require("../includes/downloads.js").main(result, filename);
  30. }
  31. else if (filename.includes("/contact"))
  32. {
  33. require("../includes/contact.js").main(request, result);
  34. }
  35. else
  36. {
  37. const html = cache.get(filename);
  38. result.writeHead(200, {'Content-Type': 'text/html'});
  39. if (html == null) {
  40. var file = "";
  41. if (filename === '' || filename === '/')
  42. {
  43. file = "../posts/homePage.js";
  44. }
  45. else
  46. {
  47. var urlSplit = filename.split("/");
  48. if (urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
  49. file = "../posts/category.js";
  50. else
  51. file = "../posts/posts.js";
  52. }
  53. Promise.all([includes.printHeader(),
  54. require(file).main(filename, request),
  55. includes.printFooter()]).then(function (content)
  56. {
  57. result.write(content.join(''));
  58. result.end();
  59. cache.put(filename, content.join(''));
  60. }).catch(function (err)
  61. {
  62. console.log(err);
  63. throw err;
  64. });
  65. }
  66. else
  67. {
  68. result.write(html);
  69. result.end();
  70. }
  71. }
  72. }
  73. };