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.

93 lines
2.1 KiB

  1. /**
  2. Utilities is a node modules created to make tasks like
  3. including html files easier for me programming.
  4. */
  5. const fs = require('fs');
  6. var Promise = require('promise');
  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 result the result that is sent to the user from node
  14. * @param fileName the file to append to the result
  15. */
  16. include: function(result, fileName)
  17. {
  18. return new Promise(function(resolve, reject)
  19. {
  20. try
  21. {
  22. result.write(fs.readFileSync(fileName));
  23. }
  24. catch (e)
  25. {
  26. console.log("Could not find " + fileName);
  27. }
  28. resolve();
  29. });
  30. },
  31. getFileContents: function(fileName)
  32. {
  33. try
  34. {
  35. return fs.readFileSync(fileName);
  36. }
  37. catch (e)
  38. {
  39. console.log("Could not find " + fileName);
  40. }
  41. return 0;
  42. },
  43. /**
  44. * Function which is responsible for returning all post data.
  45. *
  46. * @param request sent by user in initial server call
  47. * @return the post data
  48. */
  49. getPostData: function(req)
  50. {
  51. return new Promise(function(resolve, reject)
  52. {
  53. if(req.method == 'POST')
  54. {
  55. var body = '';
  56. req.on('data', function (data)
  57. {
  58. body += data;
  59. //Kills request, don't steal my RAM!!
  60. //You can only download so much ram ;)
  61. if (body.length > 1e6)
  62. {
  63. req.connection.destroy();
  64. reject();
  65. }
  66. });
  67. req.on('end', function ()
  68. {
  69. console.log(body);
  70. resolve(body);
  71. });
  72. }
  73. else
  74. {
  75. resolve(0);
  76. }
  77. });
  78. },
  79. print404: function(result)
  80. {
  81. return this.include(result, "includes/404.html");
  82. }
  83. };