// 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?