阅读时json文件行被分成两半

时间:2017-09-19 11:57:26

标签: javascript json node.js fs

我正在尝试使用下面的代码拆分json日志以获取每行的数组。似乎最后一行在阅读时被分成两半。

var stream = fs.createReadStream('web.log', {
        flags: 'r',
        encoding: 'utf-8',
        fd: null,
        mode: '0666',
        bufferSize: 64 * 1024
    });

stream.on('data', function(data){
        fileData += data;

        var lines = fileData.split('\n');
        console.log(lines);
})

日志文件如下所示:

{"name":"re","hostname":"DESKTOP","pid":1234,"level":36,"msg":"CHILD::4884","time":"2017-05-26T01:43:07.675Z","src":"","v":5}
{"name":"re","hostname":"DESKTOP","pid":4884,"level":36,"msg":"CHILD::4884","time":"2017-05-26T01:43:07.675Z","src":"","v":5}

但是分裂的结果是这样的:

[ '{"name":"re","hostname":"DESKTOP","pid":1234,"level":36,"msg":"CHILD::4884","time":"2017-05-26T01:43:07.675Z","src":"","v":5}
',
  '{"name":"re","host' ]

代码出了什么问题?

0 个答案:

没有答案