간단히 라인별로 패키지를 사용하여 읽기, 덜 두통, 더 적은 코드로 읽기 프로세스 (더 이상 멈추지 않고, 읽기를 계속할 수 있고, 파일 디스크립터를 닫고, N 라인을 건너 뛸 수 있습니다.)
1) 설치합니다.
npm i --save line-by-line
npm i --save lodash
2)
var lineByLine = require('line-by-line'),
_ = require('lodash'),
path = require('path');
var lines = [];
var filePath = path.join(__dirname, "ditLogs", file);
var fileReader = new lineByLine(filePath);
fileReader.on('line', function(line) {
line = JSON.parse(line);
if(_.isPlainObject(line) && !_.isEmpty(line)) {
lines.push(line);
}
// optional
doSomethingWithLine(line);
});
function doSomethingWithLine(line) {
// for example You can save to db or send to somewhere using request libary or just show in console
}
fileReader.on('error', function(error) {
console.error(error);
process.exit(-1);
});
fileReader.on('end', function() {
doSomethingAfterParsingAllLines(lines);
});
function doSomethingAfterParsingAllLines(records) {
// do something with data
}
'.split ("\ n")를지도 (구현 A => JSON.parse (A))'? – dandavis