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