逐行读取(Readline)

稳定度:2 - 稳定

通过 require('readline') 使用该模块。Readline 允许在逐行的基础上读取流(例如 process.stdin)。

请注意,一旦你调用此模块,直到你关闭界面前,你的 Node.js 程序将不会终止。以下代码是如何允许你的程序正常退出:

  1. const readline = require('readline');
  2. const rl = readline.createInterface({
  3. input: process.stdin,
  4. output: process.stdout
  5. });
  6. rl.question('What do you think of Node.js? ', (answer) => {
  7. // TODO: Log the answer in a database
  8. console.log('Thank you for your valuable feedback:', answer);
  9. rl.close();
  10. });