How to chain write stream, immediately with a read stream in Node.js 0.10? How to chain write stream, immediately with a read stream in Node.js 0.10? mongoose mongoose

How to chain write stream, immediately with a read stream in Node.js 0.10?


request(url).pipe(fs.createWriteStream(filename)).pipe(writestream);

is the same as this:

var fileStream = fs.createWriteStream(filename);request(url).pipe(fileStream);fileStream.pipe(writestream);

So the issue is that you are attempting to .pipe one WriteStream into another WriteStream.


// create 'fs' module variablevar fs = require("fs");// open the streamsvar readerStream = fs.createReadStream('inputfile.txt');var writerStream = fs.createWriteStream('outputfile.txt');// pipe the read and write operations// read input file and write data to output filereaderStream.pipe(writerStream);


I think the confusion in chaining the pipes is caused by the fact that the pipe method implicitly "makes choices" on it's own on what to return. That is:

readableStream.pipe(writableStream) // Returns writable streamreadableStream.pipe(duplexStream) // Returns readable stream

But the general rule says that "You can only pipe a Writable Stream to a Readable Stream." In other words only Readable Streams have the pipe() method.