Node.js Stream readable.unpipe() Method
The readable.unpipe() method in a Readable Stream is used to detach a Writable stream which was previously attached while using the stream.pipe() method.
Syntax:
readable.unpipe( destination )
Parameters: This method accepts single parameter destination which is the destination of writable stream to be detached.
Return Value: It returns stream.Writable i.e, the destination.
Below examples illustrate the use of readable.unpipe() method in Node.js:
Example 1:
// Node.js program to demonstrate the // readable.unpipe() method // Accessing fs moduleconst fs = require('fs'); // Constructing readable streamconst readable = fs.createReadStream("input.text"); // Constructing writable Streamconst writable = fs.createWriteStream("output.text"); // Calling pipe methodreadable.pipe(writable); // Calling unpipe methodreadable.unpipe(writable);console.log("done"); |
Output:
done
Example 2:
// Node.js program to demonstrate the // readable.unpipe() method // Accessing fs moduleconst fs = require('fs'); // Constructing readable streamconst readable = fs.createReadStream("input.text"); // Constructing writable Streamconst writable = fs.createWriteStream("output.text"); // All the data from readable goes into 'output.text',// for only two seconds.readable.pipe(writable);setTimeout(() => { console.log('Stop writing to output.text.'); // Calling unpipe method readable.unpipe(writable); console.log('close the file stream.'); //Calling end method writable.end();}, 2000);console.log("done"); |
Output:
done Stop writing to output.text. close the file stream.
Reference: https://nodejs.org/api/stream.html#stream_readable_unpipe_destination.



