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