美文网首页
node系列之readline

node系列之readline

作者: 一溪酒 | 来源:发表于2016-12-23 23:07 被阅读41次

地址

传送门

说明

readline模块提供了一个接口,从readable流读取一行数据,比如process.stdin。其实就是,提供了一个交互,可输入也可输出。

基本用法

const rl = readline.createInterface({ 
  input: process.stdin, 
  output: process.stdout
});

然后就是各种的监听事件。如line, 'close'等等。

rl.question('What do you think of Node.js? ', (answer) => { 
  // TODO: Log the answer in a database 
  console.log('Thank you for your valuable feedback:', answer);
  rl.close();
});

最好看的莫过于prompt了。

const rl = readline.createInterface({ 
  input: process.stdin, 
  output: process.stdout, 
  prompt: 'me> '
});
rl.prompt();
rl.on('line', (line) => { 
  //TODO your logic here
  rl.prompt();
}).on('close', () => {
  console.log('Have a great day!'); 
  process.exit(0);
});

这样就可以实现一种视觉效果

me> who you are?
me> 666

简单来说,就是多了一个前缀。

小结

readline基本没怎么用过。在用户交互方面还是有一定的作用的。可以基于这个模块来写工具。

相关文章

  • node系列之readline

    地址 传送门 说明 readline模块提供了一个接口,从readable流读取一行数据,比如process.st...

  • node.js io&web交互

    node.js io&web交互 var readline = require('readline'); var ...

  • 第7天

    1.终端和node.js的io交互var readline = require('readline');var r...

  • node之readline(逐行读取)

    require('readline') 模块提供了一个接口,用于从可读流(如 process.stdin)读取数据...

  • 8月10日Node.js的readline模块

    什么是Readline Readline是Node.js里实现标准输入输出的封装好的模块,通过这个模块我们可以以逐...

  • Node的模块之fs,readline,url

    node的模块 require:导入一个模块 exports:导出模块 module.exports:导出模块 代...

  • node 学习

    使用readline模块实现Node.js的输入输出Writing mock stdin text in your...

  • node模块 逐行读取 - readline

    模块概览 readline是个非常实用的模块。如名字所示,主要用来实现逐行读取,比如读取用户输入,或者读取文件内容...

  • 2016-08-10 readline

    nodejs readline模块 什么是readline 如何使用readline 创建readline实例 学...

  • Node.js核心模块readline

    Node.js中文网的 v6.10.3 文档提供了readline模块,可以从可读流(process.stdin)...

网友评论

      本文标题:node系列之readline

      本文链接:https://www.haomeiwen.com/subject/jtxlvttx.html