webfansplz/article

[Node.js 入门系列] 逐行读取 readline 模块

webfansplz opened this issue · 1 comments

逐行读取 readline 模块

readline 模块是一个流内容的逐行读取模块,通过 require('readline')引用模块。你可以用 readline 模块来读取 stdin,可以用来逐行读取文件流,也可用它来在控制台和用户进行一些交互。

const readline = require("readline");

const rl = readline.createInterface({
  //  监听的可读流
  input: process.stdin,
  //  逐行读取(Readline)数据要写入的可写流
  output: process.stdout
});

rl.question("你如何看待 null-cli ?", answer => {
  console.log(`感谢您的宝贵意见:${answer}`);
  rl.close();
});

readline

很多有趣的 CLI 工具是基于 readline 造的哦,有兴趣的同学也可以尝试~

上一节: [Node.js 入门系列] 流 stream 模块

下一节: [Node.js 入门系列] 查询字符串 querystring 模块

这节太少了,readline用来读取文件好像也挺好用的:

const rl = readline.createInterface({
  input: fs.createReadStream('./demo.txt')
})
rl.on('line', data => {
  console.log(data)
})
rl.on('close', () => {
  console.log('end')
})

感觉有些时候比createReadStream来设置highWaterMark限制读取大小要方便。