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.

251 lines
5.7 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. const HEADER_KEY = "header";
  10. const FOOTER_KEY = "footer";
  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. const fs = require('fs');
  22. const readFile = function(filename)
  23. {
  24. return new Promise(function(resolve, reject)
  25. {
  26. try
  27. {
  28. resolve(fs.readFileSync(filename));
  29. }
  30. catch (e)
  31. {
  32. console.log(e);
  33. console.log("Could not find " + filename);
  34. return("");
  35. }
  36. })
  37. };
  38. /**
  39. * Sends a static file to the client in a way which the web browser
  40. * caches the contents sent.
  41. *
  42. * @param cache -- server's hashmap which reduces file io
  43. * @param path -- file requested by user
  44. * @param type -- type of file for the header
  45. * @param result -- sent to client
  46. */
  47. const sendCachedContent = function(path, type, result)
  48. {
  49. const goods = cache.get(path);
  50. if(goods == null)
  51. {
  52. readFile(path).then(function(content)
  53. {
  54. const eTag = crypto.createHash('md5').update(content).digest('hex');
  55. result.writeHead(200, {'Content-Type': type, 'Cache-Control':
  56. 'public, max-age=2678400', 'ETag': '"' + eTag + '"',
  57. 'Vary': 'Accept-Encoding'});
  58. result.write(content);
  59. result.end();
  60. }).catch(function(error)
  61. {
  62. cache.del(path);
  63. console.log(error);
  64. });
  65. }
  66. else
  67. {
  68. const eTag = crypto.createHash('md5').update(goods).digest('hex');
  69. result.writeHead(200, {'Content-Type': type,
  70. 'Cache-Control': 'public, max-age=2678400',
  71. 'ETag': '"' + eTag + '"',
  72. 'Vary': 'Accept-Encoding'});
  73. result.write(goods);
  74. result.end();
  75. }
  76. };
  77. module.exports =
  78. {
  79. /** Appends the header html section to the result which is
  80. * sent to the user.
  81. *
  82. * @param result
  83. * @return {*} a promise retrieved from the utils.include function
  84. */
  85. printHeader: function(templateContext)
  86. {
  87. return module.exports.includeInObject(HEADER_KEY, templateContext, HEADER_FILE);
  88. },
  89. includeInObject: function(key, context, fileName)
  90. {
  91. return new Promise(function(resolve, reject)
  92. {
  93. readFile(fileName).then(function(result)
  94. {
  95. context[key] = result;
  96. resolve();
  97. }).catch(function(error)
  98. {
  99. context[key] = "File Not Found";
  100. reject(error);
  101. console.log(error);
  102. })
  103. });
  104. },
  105. /**
  106. * A function similar to the include statement in PHP
  107. * This function writes a file to the output
  108. *
  109. * @param fileName the file to append to the result
  110. */
  111. include: function(fileName)
  112. {
  113. return readFile(fileName);
  114. },
  115. /**
  116. * Appends the footer to the result object
  117. *
  118. * @return {*|Promise}
  119. */
  120. printFooter: function(templateContext)
  121. {
  122. return module.exports.includeInObject(FOOTER_KEY, templateContext, FOOTER_FILE);
  123. },
  124. /**
  125. * Displays the admin header
  126. *
  127. * @returns {*|Promise}
  128. */
  129. printAdminHeader(templateContext)
  130. {
  131. return module.exports.includeInObject(HEADER_KEY, templateContext, ADMIN_HEADER);
  132. },
  133. /**Sends a css file to the user
  134. *
  135. * @param result
  136. * @param path
  137. * @return {*}
  138. */
  139. sendCSS: function(result, path)
  140. {
  141. sendCachedContent(path, 'text/css', result);
  142. },
  143. /**Sends the user an image from the specified fileName.
  144. *
  145. * @param result
  146. * @param fileName
  147. */
  148. sendImage: function(result, fileName)
  149. {
  150. sendCachedContent(fileName, 'image/png', result);
  151. },
  152. //pronounced jjjif
  153. sendGif: function(result, fileName)
  154. {
  155. sendCachedContent(fileName, 'image/gif', result);
  156. },
  157. /**Sends the user an image from the specified fileName.
  158. *
  159. * @param result
  160. * @param fileName
  161. */
  162. sendJS: function(result, fileName)
  163. {
  164. sendCachedContent(fileName, 'application/javascript', result);
  165. },
  166. /** Might want to change this to be non cached later
  167. *
  168. * @param result
  169. * @param fileName
  170. */
  171. sendPDF: function(result, fileName)
  172. {
  173. sendCachedContent(fileName, 'application/pdf', result);
  174. },
  175. fetchTemplate: function(templateName)
  176. {
  177. return readFile("templates/" + templateName);
  178. },
  179. /**Sends the user an image from the specified fileName.
  180. *
  181. * @param result
  182. * @param fileName
  183. */
  184. sendHTML: function(result, fileName)
  185. {
  186. readFile(fileName).then(function(content)
  187. {
  188. result.writeHead(200, {'Content-Type': 'text/html'});
  189. result.write(content);
  190. result.end();
  191. }).catch(function(error)
  192. {
  193. console.log(error);
  194. });
  195. },
  196. /**
  197. * Sends a svg file to the client.
  198. *
  199. * @param result
  200. * @param fileName
  201. */
  202. sendSVG: function(result, fileName)
  203. {
  204. sendCachedContent(fileName, 'image/svg+xml', result);
  205. },
  206. /**
  207. * Clears the cache
  208. */
  209. clearCache: function()
  210. {
  211. console.log("Includes cache cleared");
  212. cache.clear();
  213. }
  214. };