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.

70 lines
1.9 KiB

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