[英]nodejs express stream from array
I'm building an app which i need to stream data to client, my data is simply an array of objects . 我正在构建一个需要将数据流传输到客户端的应用程序,我的数据只是一个对象数组。 this is the for loop which makes the array
这是使该数组的for循环
for(let i =0;i<files.length;i++){
try {
let file = files[i]
var musicPath = `${baseDir}/${file}`
let meta = await getMusicMeta(musicPath)
musics.push(meta)
}
right now I wait for the loop to finish it's works then I send the whole musics array to client, I want to use stream to send musics array one by one to client instead of waiting for the loop to finish 现在我等待循环完成,然后将整个音乐数组发送给客户端,我想使用流将音乐数组一个接一个地发送给客户端,而不是等待循环完成
Use scramjet and send the stream straight to the response: 使用scramjet并将流直接发送到响应:
const {DataStream} = require("scramjet");
// ...
response.writeHead(200);
DataStream.fromArray(files)
// all the magic happens below - flow control
.map(file => getMusicMeta(`${baseDir}/${file}`))
.toJSONArray()
.pipe(response);
Scramjet will make use of your flow control and most importantly - it'll get the result out faster than any other streaming framework. Scramjet将利用您的流控制,最重要的是,它将比任何其他流框架更快地获得结果。
Edit: I wrote a couple lines of code to make this use case easier in scramjet. 编辑:我写了几行代码,以使此用例在scramjet中更容易。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.