Node.js 可写流 unpipe 事件
当通过将这个 Writable 与其目标集分离来在 Readable 流上调用 stream.unpipe() 方法时,会发出 Writable Stream 中的'unpipe' 事件。
句法:
Event: 'unpipe'
返回值:如果正在调用 unpipe() 方法,则发出此事件,否则不发出。
以下示例说明了 Node.js 中“unpipe”事件的使用:
示例 1:
// Node.js program to demonstrate the
// unpipe event
// Accessing fs module
var fs = require("fs");
// Create a readable stream
var readable = fs.createReadStream('input.txt');
// Create a writable stream
var writable = fs.createWriteStream('output.txt');
// Handling unpipe event
writable.on("unpipe", readable => {
console.log("Unpiped!");
});
// Calling pipe method
readable.pipe(writable);
// Calling unpipe method
readable.unpipe(writable);
console.log("Program Ended...");
输出:
Unpiped!
Program Ended...
示例 2:
// Node.js program to demonstrate the
// unpipe event
// Accessing fs module
var fs = require("fs");
// Create a readable stream
var readable = fs.createReadStream('input.txt');
// Create a writable stream
var writable = fs.createWriteStream('output.txt');
// Handling unpipe event
writable.on("unpipe", readable => {
console.log("Unpiped!");
});
console.log("Program Ended...");
输出:
Program Ended...
因此,此处未调用 unpipe()函数,因此未发出 unpipe 事件。
参考: https://nodejs.org/api/stream.html#stream_event_unpipe