General Middleware

Read line by line from file in nodejs

Below is a simple NodeJS code that will read the file from path <Current_Directory>/filename.txt, and print each line from the file. Below execution, install the line-reader module.

npm install line-reader
const lineReader = require('line-reader');
const path = require('path')

const readFile = (req, res, next) => {
    //Get the file path using the join function
    const filePath = path.join(__dirname, 'filename.txt')
    lineReader.eachLine(filePath, function(line, last) {


        console("line = ", line)
        if (last) { //This is for the last line
            console.log("last line =", line);
            console.log("totalThreads=",totalThreads);
          } 
    })
}
module.exports = readFile

Leave a Reply

Your email address will not be published. Required fields are marked *