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.

69 lines
1.9 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. console.log("importante "+ requestURL)
  22. sql.getPost(requestURL).then(function(post)
  23. {
  24. if(post != 0)
  25. {
  26. return require("../posts/singlePost.js").renderPost(res, post);
  27. }
  28. else
  29. {
  30. return utils.print404(res);
  31. }
  32. }).then(function()
  33. {
  34. res.write("</div>");
  35. resolve();
  36. })
  37. }
  38. else
  39. {
  40. console.log("error in parameters");
  41. }
  42. });
  43. };
  44. module.exports=
  45. {
  46. /**
  47. * Calls posts and sidebar modules to render blog contents in order
  48. *
  49. * @param res
  50. * @param fileName request url
  51. */
  52. main: function(res, requestURL, request)
  53. {
  54. console.log("posts page");
  55. return new Promise(function(resolve, reject)
  56. {
  57. renderPost(res, requestURL).then(function()
  58. {
  59. return require("../sidebar/sidebar.js").main(res)
  60. }).then(function ()
  61. {
  62. resolve();
  63. });
  64. });
  65. }
  66. };