Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda