Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't we do multiple response.send in Express.js?

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

like image 733
emj365 Avatar asked Aug 28 '14 06:08

emj365


People also ask

Can I send multiple responses for a single request in Nodejs?

send() sends the response and closes the connection, whereas with response. write() you can send multiple responses.

Can Express handle multiple requests?

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.

What is the difference between response send () and response write () in Express?

send is for express and res. write+res. end() is for bare metal node ways to send data.

Why is Express JS Unopinionated?

Express JS is minimal and unopinionated​Express 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.


2 Answers

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.

like image 104
yangsibai Avatar answered Sep 28 '22 04:09

yangsibai


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.

like image 30
Robert Mitchell Avatar answered Sep 28 '22 06:09

Robert Mitchell