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.

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