gpt4 book ai didi

node.js - 早期使用 Node.js 和 Express

转载 作者:太空宇宙 更新时间:2023-11-03 23:42:26 25 4
gpt4 key购买 nike

如何使用 Express 实现早期刷新(分块传输编码)?

我发现的所有示例都涉及 http 模块,您可以在其中调用响应对象的 write() 方法,并以这种方式分段发送数据。

最佳答案

您仍然可以将 write 与 Express 结合使用:

app.get('/test', function(req, res) {
var count = 0;
var interval = setInterval(function() {
if (count++ === 5) {
clearInterval(interval);
res.end();
return;
}
res.write('This is line #' + count + '\n');
}, 1000);
});

编辑:为了正确的分块传输编码,请确保正确设置transfer-encoding header :

res.setHeader('transfer-encoding', 'chunked');

关于node.js - 早期使用 Node.js 和 Express,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20937621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com