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.

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