我正在尝试使用节点的可读流读取一个大文件(~800 MB)。我订阅了“可读”事件,在获取数据时我正在写入响应,然后等待排水事件再次读取数据。但流失事件仅被触发 3-4 次。如果有什么问题,请告诉我。
// Assume res to be any writable stream.
var readStream = fs.createReadStream('C:/movie.mp4');
readStream
.on('error', function (err) {
})
.on('readable', function () {
console.log('READABLE event fired');
writeStreamData(readStream, res);
})
.on('end', function () {
console.log('END event fired');
res.end();
});
function writeStreamData(readStream, res) {
var chunk, isFlushed;
console.log('in writeStreamData');
if (null !== (chunk = readStream.read(1024 * 1000))) {
console.log('writing data');
isFlushed = res.write(chunk);
if (!isFlushed) {
console.log('waiting for drain event');
res.once('drain', function () {
console.log('*** DRAIN event triggered');
writeStreamData(readStream, res);
});
} else {
writeStreamData(readStream, res);
}
}
}