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.

73 lines
1.8 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 Promise = require('promise');
  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. /**Sends a css file to the user
  32. *
  33. * @param result
  34. * @param path
  35. * @return {*}
  36. */
  37. sendCSS: function(result, path, cache)
  38. {
  39. var css = cache.get(path);
  40. if(css == null)
  41. {
  42. utils.include("./" + path).then(function(content)
  43. {
  44. var eTag = crypto.createHash('md5').update(content).digest('hex');
  45. result.writeHead(200, {'Content-Type': 'text/css', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"', 'Vary': 'Accept-Encoding'});
  46. result.write(content);
  47. result.end();
  48. cache.put(path, content);
  49. }).catch(function(error)
  50. {
  51. console.log(error);
  52. });
  53. }
  54. else
  55. {
  56. var eTag = crypto.createHash('md5').update(css).digest('hex');
  57. result.writeHead(200, {'Content-Type': 'text/css', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"', 'Vary': 'Accept-Encoding'});
  58. result.write(css);
  59. result.end();
  60. }
  61. }
  62. };