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.

159 lines
3.2 KiB

  1. /**
  2. Utilities is a node modules created to make tasks like
  3. including html files easier for me programming.
  4. */
  5. //used for file io
  6. const fs = require('fs');
  7. module.exports=
  8. {
  9. /**
  10. * A function similar to the include statement in PHP
  11. * This function writes a file to the output
  12. *
  13. * @param fileName the file to append to the result
  14. */
  15. include: function(fileName)
  16. {
  17. return new Promise(function(resolve, reject)
  18. {
  19. try
  20. {
  21. resolve(fs.readFileSync(fileName));
  22. }
  23. catch (e)
  24. {
  25. console.log("Could not find " + fileName);
  26. resolve("");
  27. }
  28. });
  29. },
  30. /**
  31. * Method which return the contents of a file as a string
  32. * @param fileName
  33. * @return {*}
  34. */
  35. getFileContents: function(fileName)
  36. {
  37. try
  38. {
  39. return fs.readFileSync(fileName);
  40. }
  41. catch (e)
  42. {
  43. console.log("Could not find " + fileName);
  44. }
  45. return '';
  46. },
  47. /**
  48. *
  49. * @param fileName
  50. * @returns {any}
  51. */
  52. getFileAsJSON: function(fileName)
  53. {
  54. return JSON.parse(fs.readFileSync(fileName, 'utf8'));
  55. },
  56. /**
  57. * Returns all the contents of a file as a single line
  58. * with no break lines.
  59. *
  60. * @param fileName
  61. * @return {*}
  62. */
  63. getFileLine: function(fileName)
  64. {
  65. try
  66. {
  67. return fs.readFileSync(fileName, "utf8").split('\n').join('');
  68. }
  69. catch (e)
  70. {
  71. console.log("Could not find " + fileName);
  72. }
  73. return '';
  74. },
  75. /**
  76. * Function which is responsible for returning all post data.
  77. *
  78. * @param request sent by user in initial server call
  79. * @return the post data
  80. */
  81. getPostData: function(req)
  82. {
  83. return new Promise(function(resolve, reject)
  84. {
  85. if(req.method == 'POST')
  86. {
  87. var body = '';
  88. req.on('data', function (data)
  89. {
  90. body += data;
  91. //Kills request, don't steal my RAM!!
  92. //You can only download so much ram ;)
  93. if (body.length > 1e6)
  94. {
  95. req.connection.destroy();
  96. reject();
  97. }
  98. });
  99. req.on('end', function ()
  100. {
  101. resolve(body);
  102. });
  103. }
  104. else
  105. {
  106. resolve(0);
  107. }
  108. });
  109. },
  110. /**
  111. * Displays 404 error to user
  112. *
  113. * @param result
  114. * @returns {*}
  115. */
  116. print404: function()
  117. {
  118. return this.include("includes/html/404.html");
  119. },
  120. /**
  121. * Displays 404 error to user
  122. *
  123. * @param result
  124. * @returns {*}
  125. */
  126. printWrongHost: function()
  127. {
  128. return this.include("includes/html/incorrectHost.html");
  129. },
  130. /**
  131. * Displays a ban message to the user
  132. *
  133. * @param result
  134. * @returns {*}
  135. */
  136. printBannedPage: function()
  137. {
  138. return this.include("includes/html/banHammer.html");
  139. }
  140. };