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

146
Visualizações
Changing the Response object in JavaScript
// takes the passed string as body
const response = new Response("Hello, World!");

// cannot change the body
const response = new Response();

response.body = "Hello, World!";

I am trying to implement something that is kind of like a very minimal expressjs. I want to have methods that take an empty response object as parameter and make alterations as they wish and at the end I will send that response to the client.

The response object's body is of type ReadableStream once it is created and I cannot simply set the body after that as shown above. Can I somehow change the body after that point? How does expressjs manage to do so?

My current workaround is passing a dummy response object to my methods, adding the headers I want to have in my response to that dummy object and then returning a new object with the body returned from that method and headers attached to it. But this does not look pretty or smart in any way.

The same problem occurs when I want to set the response status as well. This time I cannot use the dummy response either since that dummy's status is also read only. The workaround I found for this is again throwing some custom object from the called method and creating a new response object to be returned with that information.

Is there no way of changing the response object on the go? Is it a good practice to create HttpResponse and HttpError classes that methods can return and throw so that we can call the method and take action relative to the result of that method?

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