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
),但我不知道如何去傳遞這些額外的數據。