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.

75 lines
1.9 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':
  46. 'public, max-age=2678400', 'ETag': '"' + eTag + '"',
  47. 'Vary': 'Accept-Encoding'});
  48. result.write(content);
  49. result.end();
  50. cache.put(path, content);
  51. }).catch(function(error)
  52. {
  53. console.log(error);
  54. });
  55. }
  56. else
  57. {
  58. var eTag = crypto.createHash('md5').update(css).digest('hex');
  59. result.writeHead(200, {'Content-Type': 'text/css', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"', 'Vary': 'Accept-Encoding'});
  60. result.write(css);
  61. result.end();
  62. }
  63. }
  64. };