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.

65 lines
1.6 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(request)
  38. {
  39. console.log("Get post data method");
  40. if(request.method == 'POST')
  41. {
  42. var body = '';
  43. request.on('data', function (data)
  44. {
  45. body += data;
  46. //Kills request, don't steal my RAM!!
  47. //You can only download so much ram ;)
  48. if (body.length > 1e6)
  49. request.connection.destroy();
  50. });
  51. request.on('end', function ()
  52. {
  53. console.log(body);
  54. return body;
  55. });
  56. }
  57. return {};
  58. }
  59. };