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.

73 lines
1.9 KiB

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