Why can't we do multiple response.send in Express.js?
Maybe you need: response.write
response.write("foo");
response.write("bar");
//...
response.end()
res.send
implicitly calls res.write
followed by res.end
. If you call res.send
multiple times, it will work the first time. However, since the first res.send
call ends the response, you cannot add anything to the response.
response.send
sends an entire HTTP response to the client, including headers and content, which is why you are unable to call it multiple times. In fact, it even ends the response, so there is no need to call response.end
explicitly when using response.send
.
It appears to me that you are attempting to use send
like a buffer: writing to it with the intention to flush later. This is not how the method works, however; you need to build up your response in code and then make a single send
call.
Unfortunately, I cannot speak to why or when this change was made, but I know that it has been like this at least since Express 3.