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 sql = require('../utils/sql');
  2. const postRenderer = require('../posts/singlePost.js');
  3. /**Renders each recent post for the homepage of the website
  4. *
  5. * @param result
  6. * @returns {*|Promise}
  7. */
  8. var renderRecentPosts = function()
  9. {
  10. return new Promise(function(resolve, reject)
  11. {
  12. sql.getRecentPostSQL().then(function(posts)
  13. {
  14. var postPromises = [];
  15. var content = "<div class='col-md-8'>";
  16. posts.forEach(function(post)
  17. {
  18. postPromises.push(new Promise(function(res, rej)
  19. {
  20. postRenderer.renderPreview(post).then(function(cont)
  21. {
  22. res(cont);
  23. }).catch(function(error)
  24. {
  25. rej(error);
  26. })
  27. }));
  28. });
  29. Promise.all(postPromises).then(function(cont)
  30. {
  31. content = content + cont.join('') + "</div>";
  32. resolve(content);
  33. }).catch(function(error)
  34. {
  35. reject(error);
  36. })
  37. }).catch(function(error)
  38. {
  39. reject(error);
  40. })
  41. });
  42. };
  43. module.exports=
  44. {
  45. /**
  46. * Renders the previews of recent blog posts and the side bar
  47. *
  48. * @param res
  49. * @param fileName request url
  50. */
  51. main: function(requestURL, request)
  52. {
  53. return new Promise(function(resolve, reject)
  54. {
  55. Promise.all([renderRecentPosts(), require("../sidebar/sidebar.js").main()]).then(function(content)
  56. {
  57. resolve(content.join(''));
  58. }).catch(function(error)
  59. {
  60. reject(error);
  61. });
  62. })
  63. }
  64. };