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.8 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.include(res, "includes/404.html");
  30. }
  31. }).then(function()
  32. {
  33. res.write("</div>");
  34. resolve();
  35. })
  36. }
  37. else
  38. {
  39. console.log("error in parameters");
  40. }
  41. });
  42. };
  43. module.exports=
  44. {
  45. /**
  46. * Calls posts and sidebar modules to render blog contents in order
  47. *
  48. * @param res
  49. * @param fileName request url
  50. */
  51. main: function(res, requestURL, request)
  52. {
  53. return new Promise(function(resolve, reject)
  54. {
  55. renderPost(res, requestURL).then(function()
  56. {
  57. return require("../sidebar/sidebar.js").main(res)
  58. }).then(function ()
  59. {
  60. resolve();
  61. });
  62. });
  63. }
  64. };