3 years ago I could do multiple res.send in express.js.
even write a setTimeout to show up a live output.
response.send('<script class="jsbin" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>'); response.send('<html><body><input id="text_box" /><button>submit</button></body></html>'); var initJs = function() { $('.button').click(function() { $.post('/input', { input: $('#text_box').val() }, function() { alert('has send');}); }); } response.send('<script>' + initJs + '</script>');
Now it will throw:
Error: Can't set headers after they are sent
I know nodejs and express have updated. why can't do that now? any other idea?
Found the solution but "res.write" is not in api reference http://expressjs.com/4x/api.html ...
: S
send() sends the response and closes the connection, whereas with response. write() you can send multiple responses.
Express. js use different kinds of middleware functions in order to complete the different requests made by the client for e.g. client can make get, put, post, and delete requests these requests can easily handle by these middleware functions.
send is for express and res. write+res. end() is for bare metal node ways to send data.
Express JS is minimal and unopinionatedExpress uses less overhead in the core framework so that makes it minimal and a good choice for building out large web applications. You don't want to have a framework that fills your codebase with lots of bloatware that you are never gonna use.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With