Node.js 可读流数据事件
当调用 readable.pipe() 和 readable.resume() 方法以将流切换到流动模式或通过向数据事件添加侦听器回调时,会发出可读流中的“数据”事件。也可以通过调用 readable.read() 方法并返回可用数据块来发出此事件。
句法:
Event: 'data'
下面的例子说明了 Node.js 中数据事件的使用:
示例 1:
// Node.js program to demonstrate the
// readable data event
// Including fs module
const fs = require('fs');
// Constructing readable stream
const readable = fs.createReadStream("input.txt");
// Instructions to read data
readable.on('readable', () => {
let chunk;
// Using while loop and calling
// read method
while (null !== (chunk = readable.read())) {
// Displaying the chunk
console.log(`read: ${chunk}`);
}
});
// Handling the data event
readable.on('data', (chunk) => {
console.log(`chunk length is: ${chunk.length}`);
});
console.log("Done...");
输出:
Done...
chunk length is: 13
read: GeeksforGeeks
示例 2:
// Node.js program to demonstrate the
// readable data event
// Including fs module
const fs = require('fs');
// Constructing readable stream
const readable = fs.createReadStream("input.txt");
// Calling pause method
readable.pause();
// Handling the data event
readable.on('data', (chunk) => {
console.log(`chunk length is: ${chunk.length}`);
});
console.log("Done...");
输出:
Done...
参考: https://nodejs.org/api/stream.html#stream_event_data。