當在可讀流上調用 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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。