![](/img/trans.png)
[英]How to send real time response from server to client using Node.js express
[英]How to send real time stream response from server to client using Node.js express
在发布请求服务器上每隔几秒钟就会生成一次数据,这些几乎是 1000 到 10000 个条目。 目前我正在使用 createWriteStream 将数据保存到 csv 文件中,并且工作正常。 我如何将数据传递给客户端。
几秒钟后获取数据的姓名和年龄变量
app.post('/', (req, res) => {
// currently passing to results.csv. its working fine i want to send this real time (Name and age) data to client.
const stream = createWriteStream('./results.csv', { flags: 'a', encoding: 'utf8' })
// Append evaluation from response to file
stream.write(`${Name}, ${Age}\n`)
// example data: Patrick, 32
// End stream to avoid accumulation
stream.end()
})
res.send() only send first entry but Name and age variable getting update after after each 10 seconds
app.listen(port, () => {
console.log("App is running on port " + port);
});
快递的Response
实际上是一个steam
!
因此,您可以使用它逐块发送响应:
res.write(chunk);
并结束响应:
res.end();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.