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.

74 lines
1.8 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. /**
  32. * Function which is responsible for returning all post data.
  33. *
  34. * @param request sent by user in initial server call
  35. * @return the post data
  36. */
  37. getPostData: function(req)
  38. {
  39. return new Promise(function(resolve, reject)
  40. {
  41. if(req.method == 'POST')
  42. {
  43. var body = '';
  44. req.on('data', function (data)
  45. {
  46. body += data;
  47. //Kills request, don't steal my RAM!!
  48. //You can only download so much ram ;)
  49. if (body.length > 1e6)
  50. {
  51. req.connection.destroy();
  52. reject();
  53. }
  54. });
  55. req.on('end', function ()
  56. {
  57. console.log(body);
  58. resolve(body);
  59. });
  60. }
  61. else
  62. {
  63. resolve(0);
  64. }
  65. });
  66. }
  67. };