showing results for - "node js read file stream line by line"
Antonio
17 Apr 2019
1const readline = require('readline');
2const fs = require('fs');
3
4// create instance of readline
5// each instance is associated with single input stream
6let rl = readline.createInterface({
7    input: fs.createReadStream('products.txt')
8});
9
10let line_no = 0;
11
12// event is emitted after each line
13rl.on('line', function(line) {
14    line_no++;
15    console.log(line);
16});
17
18// end
19rl.on('close', function(line) {
20    console.log('Total lines : ' + line_no);
21});
22