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.

77 lines
2.0 KiB

  1. var Promise = require('promise');
  2. const sql = require('../utils/sql');
  3. const utils = require('../utils/utils.js');
  4. /**
  5. * Renders all posts in a single category
  6. *
  7. * @param result
  8. * @param resultURL
  9. * @returns {*}
  10. */
  11. var renderPosts = function(result, resultURL)
  12. {
  13. var splitURL = resultURL.split("/");
  14. if(splitURL.length >= 3)
  15. {
  16. result.write("<div class='col-md-8'>");
  17. return new Promise(function(resolve, reject)
  18. {
  19. sql.getPostsFromCategory(splitURL[2]).then(function(posts)
  20. {
  21. var promises = [];
  22. posts.forEach(function(p)
  23. {
  24. promises.push(new Promise(function(res, rej)
  25. {
  26. require("../posts/singlePost.js")
  27. .renderPreview(result, p).then(function()
  28. {
  29. res();
  30. });
  31. }));
  32. });
  33. return Promise.all(promises);
  34. }).then(function()
  35. {
  36. result.write("</div>");
  37. resolve();
  38. }).catch(function(err)
  39. {
  40. console.log(err);
  41. })
  42. });
  43. }
  44. else
  45. {
  46. return utils.print404(result);
  47. }
  48. };
  49. module.exports=
  50. {
  51. /**
  52. * Calls posts and sidebar modules to render blog contents in order
  53. *
  54. * @param res
  55. * @param fileName request url
  56. */
  57. main: function(res, requestURL, request)
  58. {
  59. return new Promise(function(resolve, reject)
  60. {
  61. renderPosts(res, requestURL).then(function()
  62. {
  63. return require("../sidebar/sidebar.js").main(res)
  64. }).then(function ()
  65. {
  66. resolve();
  67. }).catch(function(error)
  68. {
  69. console.log(error);
  70. reject(error);
  71. })
  72. });
  73. }
  74. }