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