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.

71 lines
1.9 KiB

  1. const utils = require('../utils/utils.js');
  2. const sql = require('../utils/sql');
  3. var Promise = require('promise');
  4. /**
  5. * Function responsible for calling the appropriate sql requests to query
  6. * database and serve correct blog post
  7. *
  8. * @param res the result sent to the client
  9. * @param requestURL url requested from client
  10. * @return {*|Promise} returns a resolved promise to preserve execution order
  11. */
  12. var renderPost = function(res, requestURL)
  13. {
  14. return new Promise(function(resolve, reject)
  15. {
  16. res.write("<div class=\"w3-col l8 s12\">");
  17. var splitURL = requestURL.split("/");
  18. //user entered /category/name/ or /category/name
  19. if(splitURL.length == 3 || splitURL.length == 4)
  20. {
  21. sql.getPost(requestURL).then(function(post)
  22. {
  23. if(post != 0)
  24. {
  25. return require("../posts/singlePost.js").renderPost(res, post);
  26. }
  27. else
  28. {
  29. return utils.print404(res);
  30. }
  31. }).then(function()
  32. {
  33. res.write("</div>");
  34. resolve();
  35. })
  36. }
  37. else
  38. {
  39. utils.print404(res).then(function()
  40. {
  41. res.write("</div>");
  42. resolve();
  43. });
  44. }
  45. });
  46. };
  47. module.exports=
  48. {
  49. /**
  50. * Calls posts and sidebar modules to render blog contents in order
  51. *
  52. * @param res
  53. * @param fileName request url
  54. */
  55. main: function(res, requestURL, request)
  56. {
  57. return new Promise(function(resolve, reject)
  58. {
  59. renderPost(res, requestURL).then(function()
  60. {
  61. return require("../sidebar/sidebar.js").main(res);
  62. }).then(function ()
  63. {
  64. resolve();
  65. });
  66. });
  67. }
  68. };