Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
Is it a valid solution to use request.socket event emitter to stop receiving or handling request body data in node js?

Notes:

#1 As far as I've read the Node Js documentation and the questions and answers on this site about how to limit the size of the request body, it's one thing to stop the current function, another thing is for the network to stop handling those packets and quite another and Aberrant is to remove the event on "data" so that it stops being handled.

#2 Since I'm not using "express" but simply the native Node Js libraries to experiment a bit, I decided to try different ways to stop the body request from handling data.

#3 Since what is received in the "data" event is a stream buffer and the length of this buffer can be read, I created an accumulator for the total stream length of the received buffer and a comparator of the length of each buffer when it is handled in order to limit handling data by these two values.

#4 The mentioned legth and limit correspond to the length of the buffer and the limite for length of the accumulator not to size in kb or mb since I plan to use it to limit the length of plain text strings.

#5 The question is: Can the "stop" event of the request socket be used to stop the handling of data and what happens in that case with network? I have done some tests and it seems to work but I have my doubts about its viability.

#6 I would like to have the opinion of people who have done something like this and possible alternatives that do not depend on libraries or external little ones.

function getBody(request, limit) {
    return new Promise((resolve, reject) => {
        try {

            let data = "";
            request.on("data", buffer => {

                if (buffer.length > limit || data.length > limit) {
                    request.socket.emit("stop");
                    resolve(null);
                } else {
                    data += buffer.toString();
                }

                request.on("end", () => {
                    resolve(data);
                });

            });
        } catch (error) {

            console.log(error);
            reject(error);
            
        }
    })
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!