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.

87 lines
2.4 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. console.log("recent post render page");
  15. sql.getRecentPostSQL().then(function(posts)
  16. {
  17. var postPromises = [];
  18. var content = "<div class='col-md-8'>";
  19. posts.forEach(function(post)
  20. {
  21. postPromises.push(new Promise(function(res, rej)
  22. {
  23. postRenderer.renderPreview(post).then(function(cont)
  24. {
  25. res(cont);
  26. }).catch(function(error)
  27. {
  28. rej(error);
  29. })
  30. }));
  31. });
  32. Promise.all(postPromises).then(function(cont)
  33. {
  34. content = content + cont.join('') + "</div>";
  35. resolve(content);
  36. }).catch(function(error)
  37. {
  38. reject(error);
  39. })
  40. }).catch(function(error)
  41. {
  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(requestURL, request)
  55. {
  56. return new Promise(function(resolve, reject)
  57. {
  58. // renderRecentPosts().then(function()
  59. // {
  60. // return require("../sidebar/sidebar.js").main();
  61. // }).then(function()
  62. // {
  63. // resolve();
  64. // }).catch(function(error)
  65. // {
  66. // console.log(error);
  67. // reject(error);
  68. // })
  69. console.log("home page");
  70. Promise.all([renderRecentPosts(), require("../sidebar/sidebar.js").main()]).then(function(content)
  71. {
  72. console.log("fin posts");
  73. resolve(content.join(''));
  74. }).catch(function(error)
  75. {
  76. reject(error);
  77. });
  78. })
  79. }
  80. };