在Express.js中使用相同的响应对象发送多个响应 [英] Sending multiple responses with the same response object in Express.js

查看:167
本文介绍了在Express.js中使用相同的响应对象发送多个响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序运行时间很长,需要分多个阶段将数据发送回去.是否可以通过 express.js

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

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

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?

推荐答案

使用res.write().

res.send()已经调用了res.end(),这意味着您在调用res.send之后就无法再写res了(这意味着您的res.end()调用也没有用).

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).

这是一个Node.js内部函数. 在此处查看文档

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

这篇关于在Express.js中使用相同的响应对象发送多个响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆