0
我正在创建一个可读流(来自包含JSON文档的文件)的数组,并且试图将它们传递给另一个流。如何在读取回调处理程序中添加附加数据和从节点流读取的数据?
在文件中的数据通过...但我收到的管道输送到流中的每个对象来了,我想知道从哪个文件中这一数据来源于:
var fs = require('fs');
var path = require('path');
var JSONStream = require('JSONStream');
var tmp1 = path.join(__dirname, 'data', 'tmp1.json');
var tmp2 = path.join(__dirname, 'data', 'tmp2.json');
var jsonStream = JSONStream.parse();
jsonStream.on('data', function (data) {
console.log('---\nFrom which file does this data come from?');
console.log(data);
});
[tmp1, tmp2].map(p => {
return fs.createReadStream(p);
}).forEach(stream => stream.pipe(jsonStream));
输出:
---
From which file does this data come from?
{ a: 1, b: 2 }
---
From which file does this data come from?
{ a: 3, b: 4 }
---
From which file does this data come from?
{ a: 5, b: 6 }
---
From which file does this data come from?
{ a: 100, b: 200 }
---
From which file does this data come from?
{ a: 300, b: 400 }
---
From which file does this data come from?
{ a: 500, b: 600 }
文件路径需要进一步处理读取的对象(在jsonStream.on('data') callback
),但我不知道如何去传递这些额外的数据。