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.

79 lines
2.1 KiB

  1. //DB query
  2. const sql = require('../utils/sql');
  3. //file IO
  4. const utils = require('../utils/utils.js');
  5. /**
  6. * Renders all posts in a single category
  7. *
  8. * @param resultURL
  9. * @returns {*}
  10. */
  11. const renderPosts = function(resultURL)
  12. {
  13. const splitURL = resultURL.split("/");
  14. if(splitURL.length >= 3)
  15. {
  16. return new Promise(function(resolve, reject)
  17. {
  18. sql.getPostsFromCategory(splitURL[2]).then(function(posts)
  19. {
  20. var promises = [];
  21. posts.forEach(function(p)
  22. {
  23. promises.push(new Promise(function(res, rej)
  24. {
  25. require("../posts/singlePost.js")
  26. .renderPreview(p).then(function(html)
  27. {
  28. res(html);
  29. }).catch(function(error)
  30. {
  31. rej(error);
  32. })
  33. }));
  34. });
  35. Promise.all(promises).then(function(content)
  36. {
  37. resolve("<div class='col-md-8'>" + content.join('') + "</div>");
  38. }).catch(function(error)
  39. {
  40. reject(error);
  41. });
  42. }).catch(function(err)
  43. {
  44. reject(err);
  45. })
  46. });
  47. }
  48. else
  49. {
  50. return utils.print404();
  51. }
  52. };
  53. module.exports=
  54. {
  55. /**
  56. * Calls posts and sidebar modules to render blog contents in order
  57. *
  58. * @param res
  59. * @param fileName request url
  60. */
  61. main: function(requestURL, request)
  62. {
  63. return new Promise(function(resolve, reject)
  64. {
  65. Promise.all([renderPosts(requestURL),
  66. require("../sidebar/sidebar.js").main()]).then(function(content)
  67. {
  68. resolve(content.join(''));
  69. }).catch(function(err)
  70. {
  71. reject(err);
  72. })
  73. });
  74. }
  75. };