我只是想了解 Node 流并尝试创建一个管道。因此读取文件并写入另一个可写流,然后再次传递到 res 因为这也是可写流。
但它抛出错误
Error [ERR_STREAM_CANNOT_PIPE]: Cannot pipe, not readable
请告诉我失败的原因,并查看下面的英特尔代码。
const http = require('http')
const fs = require('fs')
var server = http.createServer((req, res)=>{
//created the read strem
let file = fs.createReadStream('./temp_files/file1.txt')
// pipig this RES, since is RES is the aother writeable stream
let des = fs.createWriteStream('./temp_files/final.tct')
file.pipe(des).pipe(res) // ERROR <<<------------------------------
})
server.listen(9090, (req, res)=>{
console.log("runnig at port 9090")
})
最佳答案
您不能使用两个可写流来链接管道调用。您需要在中间有一个双工流。
因此替换 file.pipe(des).pipe(res) 你可以尝试以下操作:
file.pipe(des);
文件.pipe(res);
由于 des 不是转换流,因此它们可以独立工作并且不会产生任何差异。
关于javascript - 如何使a从一个管道流到另一个管道nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51269268/