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.

76 lines
2.1 KiB

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