I already using the fs.readFile API to read file but this return a buffer,but i want read file line by line,so there has some method can i do that?
You'll have to use fs(filesystem) and readline modules.
fs allows you to open a file and convert it into a stream, which you can pass to readline to read the file line by line.
As below:
var readline = require('readline');
var fs = require('fs');
var istream = fs.createReadStream('sample.txt');
var rl = readline.createInterface(istream, null);
rl.on('line', function (line) {
//whatever you want to do with each line
});
rl.close();
var through = require('through'),
fs = require('fs');
var tr = through(function (buf) {
this.queue(buf.toString());
});
var fileStream = fs.createReadStream(filename)
.pipe(tr)
.pipe(process.stdout);
Currently, as your question has no details on what you'd like to accomplish, this does nothing but output each line to stdout, but you can do whatever you want to each line inside through. If you don't need to modify the text or anything then just don't bother piping to tr.