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.

85 lines
2.1 KiB

  1. /**
  2. Includes.js
  3. File used for getting the header and footer
  4. */
  5. const utils = require('../utils/utils.js');
  6. const HEADER_FILE = "includes/header.html";
  7. const FOOTER_FILE = "includes/footer.html";
  8. const ADMIN_HEADER = "includes/adminHeader.html";
  9. const crypto = require('crypto');
  10. module.exports =
  11. {
  12. /** Appends the header html section to the result which is
  13. * sent to the user.
  14. *
  15. * @param result
  16. * @return {*} a promise retrieved from the utils.include function
  17. */
  18. printHeader: function()
  19. {
  20. return utils.include(HEADER_FILE);
  21. },
  22. /**
  23. * Appends the footer to the result object
  24. *
  25. * @return {*|Promise}
  26. */
  27. printFooter: function()
  28. {
  29. return utils.include(FOOTER_FILE);
  30. },
  31. /**
  32. * Displays the admin header
  33. *
  34. * @returns {*|Promise}
  35. */
  36. printAdminHeader()
  37. {
  38. return utils.include(ADMIN_HEADER);
  39. },
  40. /**Sends a css file to the user
  41. *
  42. * @param result
  43. * @param path
  44. * @return {*}
  45. */
  46. sendCSS: function(result, path, cache)
  47. {
  48. var css = cache.get(path);
  49. if(css == null)
  50. {
  51. utils.include("./" + path).then(function(content)
  52. {
  53. var eTag = crypto.createHash('md5').update(content).digest('hex');
  54. result.writeHead(200, {'Content-Type': 'text/css', 'Cache-Control':
  55. 'public, max-age=2678400', 'ETag': '"' + eTag + '"',
  56. 'Vary': 'Accept-Encoding'});
  57. result.write(content);
  58. result.end();
  59. cache.put(path, content);
  60. }).catch(function(error)
  61. {
  62. console.log(error);
  63. });
  64. }
  65. else
  66. {
  67. var eTag = crypto.createHash('md5').update(css).digest('hex');
  68. result.writeHead(200, {'Content-Type': 'text/css', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"', 'Vary': 'Accept-Encoding'});
  69. result.write(css);
  70. result.end();
  71. }
  72. }
  73. };