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.

67 lines
1.7 KiB

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