Open In App

Node.js Stream readable.unpipe() Method

Last Updated : 12 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report
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: javascript
// Node.js program to demonstrate the     
// readable.unpipe() method
 
// Accessing fs module
const fs = require('fs');

// Constructing readable stream
const readable = fs.createReadStream("input.text");

// Constructing writable Stream
const writable = fs.createWriteStream("output.text");

// Calling pipe method
readable.pipe(writable);

// Calling unpipe method
readable.unpipe(writable);
console.log("done");
Output:
done
Example 2: javascript
// Node.js program to demonstrate the     
// readable.unpipe() method
 
// Accessing fs module
const fs = require('fs');

// Constructing readable stream
const readable = fs.createReadStream("input.text");

// Constructing writable Stream
const 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.

Next Article

Similar Reads