admin管理员组文章数量:1316841
I am trying to emit chunks of data with a Node.js endpoint, the goal is to display it dynamically with curl. But when I use curl -X GET , nothing is displayed until everything is displayed all at once (I want the output to be dynamic, not just everything at the end). I also tried with a python flask streaming server and it works, so I guess the issue has something to do with Node.js.
Below my code :
app.get('/stream', (req, res) => {
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.setHeader('Transfer-Encoding', 'chunked');
const responseChunks = [
"Hello,",
"how are you",
"...\n"
];
let chunkIndex = 0;
// Send data every 500ms
const intervalId = setInterval(() => {
if (chunkIndex < responseChunks.length) {
res.write(responseChunks[chunkIndex]);
chunkIndex++;
} else {
clearInterval(intervalId);
res.end();
}
}, 500);
req.on('close', () => {
clearInterval(intervalId);
console.log('Stream stopped.');
});
});
...
https.createServer(credentials, app).listen(443, '0.0.0.0', () => {
console.log(`HTTPS Server is running on :443`);
});
I already checked How do I stream response in express?, but I got the same problem, the result in curl command is displayed at the end, I want the output to be dynamic.
本文标签: expressHTTPS streaming with NodejsStack Overflow
版权声明:本文标题:express - HTTPS streaming with Node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742014165a2413453.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论