Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda