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.

134 lines
3.1 KiB

  1. /**
  2. * File: Includes.js
  3. *
  4. * Module used for fetching static content for the website
  5. * like js, css, images, and other static html pages
  6. *
  7. * @author Jeffery Russell
  8. */
  9. //used for file IO
  10. const utils = require('../utils/utils.js');
  11. //name of header file
  12. const HEADER_FILE = "includes/header.html";
  13. //path of footer file
  14. const FOOTER_FILE = "includes/footer.html";
  15. //admin header path
  16. const ADMIN_HEADER = "includes/adminHeader.html";
  17. //used for hashing stuff for the header's e-tag for clients cache
  18. const crypto = require('crypto');
  19. /**
  20. * Sends a static file to the client in a way which the web browser
  21. * caches the contents sent.
  22. *
  23. * @param cache -- server's hashmap which reduces file io
  24. * @param path -- file requested by user
  25. * @param type -- type of file for the header
  26. * @param result -- sent to client
  27. */
  28. const sendCachedContent = function(cache, path, type, result)
  29. {
  30. const goods = cache.get(path);
  31. if(goods == null)
  32. {
  33. utils.include("." + path).then(function(content)
  34. {
  35. const eTag = crypto.createHash('md5').update(content).digest('hex');
  36. result.writeHead(200, {'Content-Type': type, 'Cache-Control':
  37. 'public, max-age=2678400', 'ETag': '"' + eTag + '"',
  38. 'Vary': 'Accept-Encoding'});
  39. result.write(content);
  40. result.end();
  41. cache.put(path, content);
  42. }).catch(function(error)
  43. {
  44. console.log(error);
  45. });
  46. }
  47. else
  48. {
  49. const eTag = crypto.createHash('md5').update(goods).digest('hex');
  50. result.writeHead(200, {'Content-Type': type,
  51. 'Cache-Control': 'public, max-age=2678400',
  52. 'ETag': '"' + eTag + '"',
  53. 'Vary': 'Accept-Encoding'});
  54. result.write(goods);
  55. result.end();
  56. }
  57. };
  58. module.exports =
  59. {
  60. /** Appends the header html section to the result which is
  61. * sent to the user.
  62. *
  63. * @param result
  64. * @return {*} a promise retrieved from the utils.include function
  65. */
  66. printHeader: function()
  67. {
  68. return utils.include(HEADER_FILE);
  69. },
  70. /**
  71. * Appends the footer to the result object
  72. *
  73. * @return {*|Promise}
  74. */
  75. printFooter: function()
  76. {
  77. return utils.include(FOOTER_FILE);
  78. },
  79. /**
  80. * Displays the admin header
  81. *
  82. * @returns {*|Promise}
  83. */
  84. printAdminHeader()
  85. {
  86. return utils.include(ADMIN_HEADER);
  87. },
  88. /**Sends a css file to the user
  89. *
  90. * @param result
  91. * @param path
  92. * @return {*}
  93. */
  94. sendCSS: function(result, path, cache)
  95. {
  96. sendCachedContent(cache, "/" + path, 'text/css', result);
  97. },
  98. /**Sends the user an image from the specified fileName.
  99. *
  100. * @param result
  101. * @param fileName
  102. */
  103. sendImage: function(result, fileName, cache)
  104. {
  105. sendCachedContent(cache, fileName, 'image/png', result);
  106. },
  107. /**Sends the user an image from the specified fileName.
  108. *
  109. * @param result
  110. * @param fileName
  111. */
  112. sendJS: function(result, fileName, cache)
  113. {
  114. sendCachedContent(cache, fileName, 'application/javascript', result);
  115. }
  116. };