当在可读流上调用 stream.unpipe() 方法时,可写流中的 ‘unpipe’ 事件会被发出,方法是将这个 Writable 与其目标集分离。
用法:
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
相关用法
- node.js Stream writable.writable用法及代码示例
- node.js Stream readable.unpipe()用法及代码示例
- Node.js Writable Stream pipe事件用法及代码示例
- Node.js Writable Stream finish事件用法及代码示例
- node.js Stream writable.writableFinished用法及代码示例
- node.js Stream writable.writableObjectMode用法及代码示例
- node.js Stream writable.writableLength用法及代码示例
- node.js Stream writable.cork()用法及代码示例
- node.js Stream writable.writableHighWaterMark用法及代码示例
- node.js Stream writable.writableCorked用法及代码示例
- node.js Stream writable.write()用法及代码示例
- node.js Stream writable.end()用法及代码示例
- node.js Stream writable.writableEnded用法及代码示例
- node.js Stream writable.destroyed用法及代码示例
- node.js Stream writable.destroy()用法及代码示例
- node.js Stream writable.uncork()用法及代码示例
- node.js Stream writable.setDefaultEncoding()用法及代码示例
- Node.js Readable Stream readable事件用法及代码示例
- Node.js Readable Stream end事件用法及代码示例
- Node.js Readable Stream data事件用法及代码示例
- Node.js Readable Stream close事件用法及代码示例
注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 Node.js | Writable Stream unpipe Event。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。