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

145
Views
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
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!