How to read from stdin line by line in Node

前端 未结 8 1421
闹比i
闹比i 2020-11-28 01:47

I\'m looking to process a text file with node using a command line call like:

node app.js < input.txt

Each line of the file needs to be proce

8条回答
  •  無奈伤痛
    2020-11-28 02:23

    readline is specifically designed to work with terminal (that is process.stdin.isTTY === true). There are a lot of modules which provide split functionality for generic streams, like split. It makes things super-easy:

    process.stdin.pipe(require('split')()).on('data', processLine)
    
    function processLine (line) {
      console.log(line + '!')
    }
    

提交回复
热议问题