【发布时间】:2021-11-06 22:17:01
【问题描述】:
我想使用 nodejs 中的流发送数据。我尝试使用管道发送数据,但没有成功,请帮助我。
const { Readable } = require("stream")
app.get('/test',async (req,res)=>{
let month = ['jan','feb','mar','apr','may','jun','jul'];
let stream = new Readable({read(size){}});
stream.setEncoding('utf8')
for(let m of month) {
stream.push(m)
}
stream.on('data',(data) => {
console.log(data)
res.write(data,'utf8')
})
stream.on('end',() => {
res.end();
})
})
我也试过了,但是没用……
const { 可读 } = require("stream")
app.get('/test',async (req,res)=>{
let month = ['jan','feb','mar','apr','may','jun','jul'];
let stream = new Readable({read(size){}});
stream.setEncoding('utf8')
for(let m of month) {
stream.push(m)
}
stream.pipe(res)
})
【问题讨论】:
-
您遇到了哪些错误(如果有)?
-
EOL- 我想使用管道以块的形式发送数据。我尝试了解决方案,然后是您的链接,但它不起作用