node.js, read from file line by line

Read line by line from a file.

function processFile(inputFile) {
    var fs = require('fs'),
        readline = require('readline'),
        instream = fs.createReadStream(inputFile),
        outstream = new (require('stream'))(),
        rl = readline.createInterface(instream, outstream);
    
    rl.on('line', function (line) {
        console.log(line);
    });
   
    rl.on('close', function (line) {
        console.log(line);
        console.log('done reading file.');
    });
}
processFile('/path/to/a/input/file.txt');

Read the entire file in utf8 format.

fs.readFile('input.txt', 'utf8', function (err, data) {
  if (err) throw err;
  console.log(data);
});

Search within Codexpedia

Custom Search

Search the entire web

Custom Search