當在可讀流上調用 stream.pipe() 方法時,可寫流中的管道事件會通過將此可寫對象附加到其目標集來調用。
用法:
Event:'pipe'
返回值:如果正在調用 pipe() 方法,則發出此事件,否則不發出。
下麵的示例說明了 Node.js 中 ‘pipe’ 事件的使用:示例 1:
// Node.js program to demonstrate the
// pipe 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 pipe event
writable.on("pipe", readable => {
console.log("Piped!");
});
// Calling pipe method
readable.pipe(writable);
console.log("Program Ended.");
輸出:
Piped! Program Ended.
範例2:
// Node.js program to demonstrate the
// pipe 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 pipe event
writable.on("pipe", readable => {
console.log("Piped!");
});
console.log("Program Ended");
輸出:
Program Ended
因此,此處未調用 pipe() 函數,因此不會發出管道事件。
參考: https://nodejs.org/api/stream.html#stream_event_pipe
相關用法
- node.js Stream writable.writable用法及代碼示例
- Node.js Writable Stream finish事件用法及代碼示例
- Node.js Writable Stream unpipe事件用法及代碼示例
- node.js Stream readable.pipe()用法及代碼示例
- 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()用法及代碼示例
- jQuery deferred.pipe()用法及代碼示例
- Collect.js pipe()用法及代碼示例
- Node.js Readable Stream readable事件用法及代碼示例
- Node.js Readable Stream end事件用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | Writable Stream pipe Event。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。