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.

71 lines
1.9 KiB

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