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