在Express.js中使用相同的响应对象发送多个响应

我有一个漫长的运行过程,需要在多个阶段发回数据。 有没有办法用express.js发回多个回复

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

但是当我运行curl -X POST localhost:3001/helloworld我得到的是hello

我怎样才能发送多个回复,或者这不可能与快递?

使用res.write()

res.send()已经调用了res.end() ,这意味着在调用res.send(也意味着你的res.end()调用是无用的)之后,你不能再写入res了。

一个HTTP请求只能发送一个HTTP响应。 但是,您肯定可以在所需的响应中编写任何types的数据。 这可能是换行符分隔的JSON,多部分,或其他任何你select的格式。

如果你想把事件从服务器传送到浏览器,一个简单的select可能是使用像服务器发送的事件 ( polyfill )。

试试这个,这个应该可以解决你的问题。

 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(); });