19

I have a long running process which needs to send data back at multiple stages. Is there some way to send back multiple responses with express.js

res.send(200, 'hello')
res.send(200, 'world')
res.end() 

but when I run curl -X POST localhost:3001/helloworld all I get is hello

How can I send multiple responses or is this not possible to do with express?

Loourr
  • 4,995
  • 10
  • 44
  • 68

4 Answers4

33

Use res.write().

res.send() already makes a call to res.end(), meaning you can't write to res anymore after a call to res.send (meaning also your res.end() call was useless).

EDIT: It is a Node.js internal function. See the documentation here

yachaka
  • 5,429
  • 1
  • 23
  • 35
  • I can't find that function in the [documentation](http://expressjs.com/4x/api.html) and it doesn't seem to be working for me. – Loourr Aug 08 '14 at 17:45
  • It is not from express, but directly from Node.js. It is the low level function. What do you try to achieve ? This should work as expected for the example you described ahead, I just tried it in local. – yachaka Aug 08 '14 at 17:55
  • @aydow Links or it did not happened – codebot Feb 24 '20 at 10:23
  • @codebot, all i can see from the [documentation](https://expressjs.com/en/api.html#res) is that `res.write()` is not there – aydow Feb 25 '20 at 22:18
  • 1
    @aydow `res.write()` is a Node.js function, [see the documentation here](https://nodejs.org/dist/latest-v13.x/docs/api/http.html#http_response_write_chunk_encoding_callback) – yachaka Feb 26 '20 at 10:16
  • thanks for the clarification, guys. apologies for the confusion! – aydow Mar 05 '20 at 05:38
  • Why can't I write `res.json()` multiple times instead of `res.write()` in **express** ? As I want to send multiple responses as in a json format from the same api request. – A K Mar 30 '21 at 10:51
  • How to handle multiple response in front-end part. – aakash4dev Apr 13 '22 at 11:54
  • 1
    @aydow `res.write()` isn't deprecated. I suggest removing your comment lest it mislead someone. – ggorlen May 20 '22 at 01:53
  • @aakash4dev I have an example of streaming server-sent events from a client in [How to use server-sent-events in express.js](https://stackoverflow.com/a/67184841/6243352). – ggorlen May 20 '22 at 01:55
7

You can only send one HTTP response for one HTTP request. However, you can certainly write whatever kind of data in the response that you want. That could be newline-delimited JSON, multipart parts, or whatever other format you choose.

If you want to stream events from the server to the browser, an easy alternative might be to use something like Server-sent events (polyfill).

mscdex
  • 104,356
  • 15
  • 192
  • 153
2

Try this, this should solve your problem.

app.get('/', function (req, res) {

  var i = 1,
    max = 5;

  //set the appropriate HTTP header
  res.setHeader('Content-Type', 'text/html');

  //send multiple responses to the client
  for (; i <= max; i++) {
    res.write('<h1>This is the response #: ' + i + '</h1>');
  }

  //end the response process
  res.end();
});
patz
  • 1,306
  • 4
  • 25
  • 42
0
res.write(JSON.stringify({
    min, 
    max, 
    formattedData
}));

or

res.send({
    min,
    max,
    formattedData
});

refer Node Res.write send multiple objects:

zmag
  • 7,825
  • 12
  • 32
  • 42