Node.js Writable Stream pipe Event
The pipe event in a Writable Stream is emitted when the stream.pipe() method is being called on a readable stream by attaching this writable to its set of destinations.
Syntax:
Event: 'pipe'
Return Value: If the pipe() method is being called then this event is emitted else it is not emitted.
The below examples illustrate the use of the ‘pipe’ event in Node.js:
Example 1:
javascript
const fs = require( "fs" );
const readable = fs.createReadStream( 'input.txt' );
const writable = fs.createWriteStream( 'output.txt' );
writable.on( "pipe" , readable => {
console.log( "Piped!" );
});
readable.pipe(writable);
console.log( "Program Ended." );
|
Output:
Piped!
Program Ended.
Example 2:
javascript
const fs = require( "fs" );
const readable = fs.createReadStream( 'input.txt' );
const writable = fs.createWriteStream( 'output.txt' );
writable.on( "pipe" , readable => {
console.log( "Piped!" );
});
console.log( "Program Ended" );
|
Output:
Program Ended
So, here pipe() function is not called so the pipe event is not emitted.
Reference: https://nodejs.org/api/stream.html#stream_event_pipe
Last Updated :
30 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...