read document line by line javascript code example
Example 1: readline in javascript
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('What do you think of Node.js? ', (answer) => {
console.log(`Thank you for your valuable feedback: ${answer}`);
rl.close();
});
Example 2: node js read file stream line by line
const readline = require('readline');
const fs = require('fs');
let rl = readline.createInterface({
input: fs.createReadStream('products.txt')
});
let line_no = 0;
rl.on('line', function(line) {
line_no++;
console.log(line);
});
rl.on('close', function(line) {
console.log('Total lines : ' + line_no);
});