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.

173 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. console.log(content);
  43. result.end();
  44. cache.put(path, content);
  45. }).catch(function(error)
  46. {
  47. console.log(error);
  48. });
  49. }
  50. else
  51. {
  52. const eTag = crypto.createHash('md5').update(goods).digest('hex');
  53. result.writeHead(200, {'Content-Type': type,
  54. 'Cache-Control': 'public, max-age=2678400',
  55. 'ETag': '"' + eTag + '"',
  56. 'Vary': 'Accept-Encoding'});
  57. result.write(goods);
  58. result.end();
  59. }
  60. };
  61. module.exports =
  62. {
  63. /** Appends the header html section to the result which is
  64. * sent to the user.
  65. *
  66. * @param result
  67. * @return {*} a promise retrieved from the utils.include function
  68. */
  69. printHeader: function()
  70. {
  71. return utils.include(HEADER_FILE);
  72. },
  73. /**
  74. * Appends the footer to the result object
  75. *
  76. * @return {*|Promise}
  77. */
  78. printFooter: function()
  79. {
  80. return utils.include(FOOTER_FILE);
  81. },
  82. /**
  83. * Displays the admin header
  84. *
  85. * @returns {*|Promise}
  86. */
  87. printAdminHeader()
  88. {
  89. return utils.include(ADMIN_HEADER);
  90. },
  91. /**Sends a css file to the user
  92. *
  93. * @param result
  94. * @param path
  95. * @return {*}
  96. */
  97. sendCSS: function(result, path, cache)
  98. {
  99. sendCachedContent(path, 'text/css', result);
  100. },
  101. /**Sends the user an image from the specified fileName.
  102. *
  103. * @param result
  104. * @param fileName
  105. */
  106. sendImage: function(result, fileName)
  107. {
  108. console.log(fileName);
  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. /**Sends the user an image from the specified fileName.
  121. *
  122. * @param result
  123. * @param fileName
  124. */
  125. sendHTML: function(result, fileName)
  126. {
  127. utils.include("." + fileName).then(function(content)
  128. {
  129. console.log(fileName);
  130. console.log(content);
  131. result.writeHead(200, {'Content-Type': 'text/html'});
  132. result.write(content);
  133. result.end();
  134. }).catch(function(error)
  135. {
  136. console.log(error);
  137. });
  138. },
  139. /**
  140. * Sends a svg file to the client.
  141. *
  142. * @param result
  143. * @param fileName
  144. */
  145. sendSVG: function(result, fileName)
  146. {
  147. sendCachedContent(fileName, 'image/svg+xml', result);
  148. }
  149. };