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. var Promise = require('promise');
  2. const sql = require('../utils/sql');
  3. const utils = require('../utils/utils.js');
  4. var renderPosts = function(result, resultURL)
  5. {
  6. var splitURL = resultURL.split("/");
  7. if(splitURL.length >= 3)
  8. {
  9. result.write("<div class=\"w3-col l8 s12\">\n");
  10. return new Promise(function(resolve, reject)
  11. {
  12. sql.getPostsFromCategory(splitURL[2]).then(function(posts)
  13. {
  14. var promises = [];
  15. posts.forEach(function(p)
  16. {
  17. promises.push(new Promise(function(res, rej)
  18. {
  19. require("../posts/singlePost.js")
  20. .renderPreview(result, p).then(function()
  21. {
  22. res();
  23. });
  24. }));
  25. });
  26. return Promise.all(promises);
  27. }).then(function()
  28. {
  29. result.write("</div>");
  30. resolve();
  31. }).catch(function(err)
  32. {
  33. console.log(err);
  34. })
  35. });
  36. }
  37. else
  38. {
  39. return utils.print404(result);
  40. }
  41. };
  42. module.exports=
  43. {
  44. /**
  45. * Calls posts and sidebar modules to render blog contents in order
  46. *
  47. * @param res
  48. * @param fileName request url
  49. */
  50. main: function(res, requestURL, request)
  51. {
  52. return new Promise(function(resolve, reject)
  53. {
  54. renderPosts(res, requestURL).then(function()
  55. {
  56. return require("../sidebar/sidebar.js").main(res)
  57. }).then(function ()
  58. {
  59. resolve();
  60. }).catch(function(error)
  61. {
  62. console.log(error);
  63. reject(error);
  64. })
  65. });
  66. }
  67. }