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.

178 lines
4.0 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/html/header.html";
  13. //path of footer file
  14. const FOOTER_FILE = "includes/html/footer.html";
  15. //admin header path
  16. const ADMIN_HEADER = "includes/html/adminHeader.html";
  17. //used for hashing stuff for the header's e-tag for clients cache
  18. const crypto = require('crypto');
  19. //caching program to make the application run faster
  20. const cache = require('memory-cache');
  21. /**
  22. * Sends a static file to the client in a way which the web browser
  23. * caches the contents sent.
  24. *
  25. * @param cache -- server's hashmap which reduces file io
  26. * @param path -- file requested by user
  27. * @param type -- type of file for the header
  28. * @param result -- sent to client
  29. */
  30. const sendCachedContent = function(path, type, result)
  31. {
  32. const goods = cache.get(path);
  33. if(goods == null)
  34. {
  35. utils.include("." + path).then(function(content)
  36. {
  37. const eTag = crypto.createHash('md5').update(content).digest('hex');
  38. result.writeHead(200, {'Content-Type': type, 'Cache-Control':
  39. 'public, max-age=2678400', 'ETag': '"' + eTag + '"',
  40. 'Vary': 'Accept-Encoding'});
  41. result.write(content);
  42. result.end();
  43. cache.put(path, content);
  44. }).catch(function(error)
  45. {
  46. console.log(error);
  47. });
  48. }
  49. else
  50. {
  51. const eTag = crypto.createHash('md5').update(goods).digest('hex');
  52. result.writeHead(200, {'Content-Type': type,
  53. 'Cache-Control': 'public, max-age=2678400',
  54. 'ETag': '"' + eTag + '"',
  55. 'Vary': 'Accept-Encoding'});
  56. result.write(goods);
  57. result.end();
  58. }
  59. };
  60. module.exports =
  61. {
  62. /** Appends the header html section to the result which is
  63. * sent to the user.
  64. *
  65. * @param result
  66. * @return {*} a promise retrieved from the utils.include function
  67. */
  68. printHeader: function()
  69. {
  70. return utils.include(HEADER_FILE);
  71. },
  72. /**
  73. * Appends the footer to the result object
  74. *
  75. * @return {*|Promise}
  76. */
  77. printFooter: function()
  78. {
  79. return utils.include(FOOTER_FILE);
  80. },
  81. /**
  82. * Displays the admin header
  83. *
  84. * @returns {*|Promise}
  85. */
  86. printAdminHeader()
  87. {
  88. return utils.include(ADMIN_HEADER);
  89. },
  90. /**Sends a css file to the user
  91. *
  92. * @param result
  93. * @param path
  94. * @return {*}
  95. */
  96. sendCSS: function(result, path, cache)
  97. {
  98. sendCachedContent(path, 'text/css', result);
  99. },
  100. /**Sends the user an image from the specified fileName.
  101. *
  102. * @param result
  103. * @param fileName
  104. */
  105. sendImage: function(result, fileName)
  106. {
  107. sendCachedContent(fileName, 'image/png', result);
  108. },
  109. /**Sends the user an image from the specified fileName.
  110. *
  111. * @param result
  112. * @param fileName
  113. */
  114. sendJS: function(result, fileName)
  115. {
  116. sendCachedContent(fileName, 'application/javascript', result);
  117. },
  118. /**Sends the user an image from the specified fileName.
  119. *
  120. * @param result
  121. * @param fileName
  122. */
  123. sendHTML: function(result, fileName)
  124. {
  125. utils.include("." + fileName).then(function(content)
  126. {
  127. result.writeHead(200, {'Content-Type': 'text/html'});
  128. result.write(content);
  129. result.end();
  130. }).catch(function(error)
  131. {
  132. console.log(error);
  133. });
  134. },
  135. /**
  136. * Sends a svg file to the client.
  137. *
  138. * @param result
  139. * @param fileName
  140. */
  141. sendSVG: function(result, fileName)
  142. {
  143. sendCachedContent(fileName, 'image/svg+xml', result);
  144. },
  145. /**
  146. * Clears the cache
  147. */
  148. clearCache: function()
  149. {
  150. console.log("Includes cache cleared");
  151. cache.clear();
  152. }
  153. };