Node.js gets the content of the txt file line by line

Paste the code directly:  

const readline = require('readline') 
//Readline is a packaged module that implements standard input and output in Node.js. Through this module, we can read the data stream line by line. Modules can be referenced using require("readline").
const r1 = readline.createInterface({
input: fs.createReadStream("data.txt")
});
var i = 1; //Number of lines in txt 
r1.on('line', function(line){ //Event listener
console.log('Line from file:' + i + ":" + line);
  if (i == 1){
    console.log(line)
  }
  i+=1;
})
This operation is performed asynchronously,

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326154059&siteId=291194637