CNode

为什么fs.createReadStream不能pipe多次?

问答
Wwangyangkobe发布于11 年前最后回复11 年前5 回复8441 浏览0 收藏

Error: Cannot pipe. Not readable. at WriteStream.Writable.pipe (_stream_writable.js:162:22) at repl:1:86 at REPLServer.defaultEval (repl.js:132:27) at bound (domain.js:254:14) at REPLServer.runBound [as eval] (domain.js:267:12) at REPLServer. (repl.js:279:12) at REPLServer.emit (events.js:107:17) at REPLServer.Interface._onLine (readline.js:214:10) at REPLServer.Interface._line (readline.js:553:8) at REPLServer.Interface._ttyWrite (readline.js:830:14)

查看回复

回复 (5)

1
1000copy#1·11 年前

这样可以:

var f = fs.createReadStream('...') 
f.pipe(fs.createWriteStream('...'))
f.pipe(fs.createWriteStream('...')

你那样不行的原因是,第一次pipe后,返回的类型是WriteStream ,而不再是ReadStream .看看:

f.pipe(fs.createWriteStream('readme2.md')).constructor
>{ [Function: WriteStream]


W
wangyangkobe#2·11 年前

@1000copy 说的有道理。

S
songqinghehe#3·11 年前

mark

J
jifeng#4·11 年前

一楼正解

K
kyriosli#5·11 年前

只有Duplex类型才能连续pipe,比如zlib.createGzip()socket

参与回复
登录后即可参与回复。登录