Javascript 在Express.js中使用相同的响应对象发送多个响应
我有一个长时间运行的过程,需要在多个阶段发送数据。是否有某种方法可以将多个响应发送回 但是当我运行Javascript 在Express.js中使用相同的响应对象发送多个响应,javascript,node.js,express,Javascript,Node.js,Express,我有一个长时间运行的过程,需要在多个阶段发送数据。是否有某种方法可以将多个响应发送回 但是当我运行curl-X POST localhost:3001/helloworld时,我得到的只是hello 我如何才能发送多个响应,或者这与express无关?使用res.write() res.send() 编辑:它是一个Node.js内部函数 一个HTTP请求只能发送一个HTTP响应。但是,您当然可以在响应中写入所需的任何类型的数据。这可以是换行符分隔的JSON、多部分部分或您选择的任何其他格式 如果
curl-X POST localhost:3001/helloworld
时,我得到的只是hello
我如何才能发送多个响应,或者这与express无关?使用res.write()
res.send()
编辑:它是一个Node.js内部函数 一个HTTP请求只能发送一个HTTP响应。但是,您当然可以在响应中写入所需的任何类型的数据。这可以是换行符分隔的JSON、多部分部分或您选择的任何其他格式
如果您想将事件从服务器流式传输到浏览器,一个简单的替代方法可能是使用类似()的内容。试试这个,这会解决您的问题
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();
});
app.get('/',函数(req,res){
var i=1,
最大值=5;
//设置适当的HTTP头
res.setHeader('Content-Type','text/html');
//向客户端发送多个响应
对于(;ires.write(JSON.stringify({
闵,
最大值,
格式化数据
}));
或
res.send({
闵,
最大值,
格式化数据
});
请参阅我在中找不到该函数,它似乎对我不起作用。它不是来自express,而是直接来自Node.js。它是低级函数。您试图实现什么?对于前面描述的示例,这应该可以正常工作,我只是在本地尝试过。res.write()
从express 3开始,似乎已弃用。x@aydow链接或它没有happened@aydowres.write()
是一个Node.js函数,
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();
});