2012-11-16 65 views
3

我正在閱讀使用JSONStream的大型JSON文件,並且我想在處理整個流時調用方法。使用事件流結束處理流

var JSONStream = require('JSONStream'), es = require('event-stream'); 

es.pipeline(
    fs.createReadStream('file.json'), 
    JSONStream.parse(['features', true]), 
    es.map(function (data) { 
    console.log("Added data"); 
    }) 
); 

我該怎麼做?

回答

1

Sorted。將讀取的流保存在一個變量中,並在該讀取流上輸入('end',function)。

2

我使用'event-stream'來處理〜200kB文件,裏面包含JSONs,並在'end'時得到了問題當使用'event-stream'時,如果我把.on('end')從來沒有調用過事件事件流管道之後。但是當我把它放在管道之前 - 一切正常!

stream.on('end',function() { 
      console.log("This is the End, my friend");     
    }).on('error',function (err) { 
       console.error(err); 
    }).pipe(es.split()) 
     .pipe(es.map(function (line, cb) { 
       //Do anything you want here with JSON of line 
       return cb(); 
      }));