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.

152 lines
3.3 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. includeInObject: function(key, context, fileName)
  31. {
  32. return new Promise(function(resolve, reject)
  33. {
  34. module.exports.include(fileName).then(function(result)
  35. {
  36. context[key] = result;
  37. resolve();
  38. }).catch(function(error)
  39. {
  40. context[key] = "File Not Found";
  41. reject(error);
  42. console.log(error);
  43. })
  44. })
  45. },
  46. /**
  47. * Method which return the contents of a file as a string
  48. * @param fileName
  49. * @return {*}
  50. */
  51. getFileContents: function(fileName)
  52. {
  53. try
  54. {
  55. return fs.readFileSync(fileName);
  56. }
  57. catch (e)
  58. {
  59. console.log("Could not find " + fileName);
  60. }
  61. return '';
  62. },
  63. /**
  64. *
  65. * @param fileName
  66. * @returns {any}
  67. */
  68. getFileAsJSON: function(fileName)
  69. {
  70. return JSON.parse(fs.readFileSync(fileName, 'utf8'));
  71. },
  72. /**
  73. * Returns all the contents of a file as a single line
  74. * with no break lines.
  75. *
  76. * @param fileName
  77. * @return {*}
  78. */
  79. getFileLine: function(fileName)
  80. {
  81. try
  82. {
  83. return fs.readFileSync(fileName, "utf8").split('\n').join('');
  84. }
  85. catch (e)
  86. {
  87. console.log("Could not find " + fileName);
  88. }
  89. return '';
  90. },
  91. /**
  92. * Function which is responsible for returning all post data.
  93. *
  94. * @param request sent by user in initial server call
  95. * @return the post data
  96. */
  97. getPostData: function(req)
  98. {
  99. return new Promise(function(resolve, reject)
  100. {
  101. if(req.method == 'POST')
  102. {
  103. var body = '';
  104. req.on('data', function (data)
  105. {
  106. body += data;
  107. //Kills request, don't steal my RAM!!
  108. //You can only download so much ram ;)
  109. if (body.length > 1e6)
  110. {
  111. req.connection.destroy();
  112. reject();
  113. }
  114. });
  115. req.on('end', function ()
  116. {
  117. resolve(body);
  118. });
  119. }
  120. else
  121. {
  122. resolve(0);
  123. }
  124. });
  125. },
  126. /**
  127. * Displays 404 error to user
  128. *
  129. * @param result
  130. * @returns {*}
  131. */
  132. print404: function()
  133. {
  134. return this.include("includes/html/404.html");
  135. }
  136. };