Advertisement
retesere20

old-nodejs-server-snippet

Aug 1st, 2022
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1.  
  2. const server = http.createServer();
  3.  
  4. endResponse(str){
  5. this.response.write(str);
  6. this.response.end("");
  7. },
  8. dieIfBadAccess(request)
  9. {
  10. var body = '';
  11. var self = this;
  12. request.on('data', function (data) {
  13. body += data;
  14. // 1e6 === 1 * Math.pow(10, 5) === 1 * 100000 ~~~ 100kb
  15. if (body.length > 1e5) {
  16. // FLOOD ATTACK OR FAULTY CLIENT, NUKE REQUEST
  17. request.connection.destroy();
  18. self.responseEND();//maybe not needed
  19. }
  20. });
  21. return body;
  22. },
  23.  
  24. setResponseVariable(resp){
  25. this.responseGLOBAL = resp;
  26. },
  27. echo (content){
  28. if (!this.responseGLOBAL.finished) this.responseGLOBAL.write(content); else c(content);
  29. },
  30.  
  31. responseError(str) { this.output = { status: "error", response:str }; this.responseEcho(); this.responseEND(); },
  32. responseSuccess(str){ this.output = { status: "success", response:str }; this.responseEcho(); this.responseEND(); },
  33. error (num, extra) { this.responseError(this.MessageCodes(num,extra)); },
  34. success(str) { this.responseSuccess(str); },
  35.  
  36. responseEcho(str){ this.echo( JSON.stringify(this.output) ); },
  37.  
  38. responseEnded:false,
  39. responseEND(){ this.responseGLOBAL.end(""); },
  40.  
  41. start_server()
  42. {
  43. const self = this;
  44. server.on('request', function (request, response) {
  45. try {
  46. if (request.url == '/favicon.ico') return;
  47.  
  48. var data = self.dieIfBadAccess(request);
  49.  
  50. if (!('act' in query)) {
  51. this.responseError("no action");
  52. }
  53. else {
  54. var { headers, method, url } = request;
  55. await this.create_responserPage();
  56. }
  57. }
  58. catch (exc) {
  59. c(exc);
  60. }
  61. });
  62. server.listen(this.Server_Port, host);
  63. console.log(`Server running at ${this.Server_Url}`);
  64. },
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement