当在可读流上调用 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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。