模块概览

readline是个非常实用的模块。如名字所示,主要用来实现逐行读取,比如读取用户输入,或者读取文件内容。常见使用场景有下面几种,本文会逐一举例说明。

  • 文件逐行读取:比如说进行日志分析。
  • 自动完成:比如输入npm,自动提示”help init install”。
  • 命令行工具:比如npm init这种问答式的脚手架工具。

基础例子

先看个简单的例子,要求用户输入一个单词,然后自动转成大写

  1. const readline = require('readline');
  2. const rl = readline.createInterface({
  3. input: process.stdin,
  4. output: process.stdout
  5. });
  6. rl.question('Please input a word: ', function(answer){
  7. console.log('You have entered [%s]', answer.toUpperCase());
  8. rl.close();
  9. });

运行如下:

  1. toUpperCase git:(master) node app.js
  2. Please input a word: hello
  3. You have entered {HELLO}

例子:文件逐行读取:日志分析

比如我们有如下日志文件access.log,我们想要提取“访问时间+访问地址”,借助readline可以很方便的完成日志分析的工作。

  1. [2016-12-09 13:56:48.407] [INFO] access - ::ffff:127.0.0.1 - - "GET /oc/v/account/user.html HTTP/1.1" 200 213125 "http://www.example.com/oc/v/account/login.html" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36"
  2. [2016-12-09 14:00:10.618] [INFO] access - ::ffff:127.0.0.1 - - "GET /oc/v/contract/underlying.html HTTP/1.1" 200 216376 "http://www.example.com/oc/v/account/user.html" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36"
  3. [2016-12-09 14:00:34.200] [INFO] access - ::ffff:127.0.0.1 - - "GET /oc/v/contract/underlying.html HTTP/1.1" 200 216376 "http://www.example.com/oc/v/account/user.html" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36"

代码如下:

  1. const readline = require('readline');
  2. const fs = require('fs');
  3. const rl = readline.createInterface({
  4. input: fs.createReadStream('./access.log')
  5. });
  6. rl.on('line', (line) => {
  7. const arr = line.split(' ');
  8. console.log('访问时间:%s %s,访问地址:%s', arr[0], arr[1], arr[13]);
  9. });

运行结果如下:

  1. lineByLineFromFile git:(master) node app.js
  2. 访问时间:[2016-12-09 13:56:48.407],访问地址:"http://www.example.com/oc/v/account/login.html"
  3. 访问时间:[2016-12-09 14:00:10.618],访问地址:"http://www.example.com/oc/v/account/user.html"
  4. 访问时间:[2016-12-09 14:00:34.200],访问地址:"http://www.example.com/oc/v/account/user.html"

例子:自动完成:代码提示

这里我们实现一个简单的自动完成功能,当用户输入npm时,按tab键,自动提示用户可选的子命令,如help、init、install。

  • 输入np,按下tab:自动补全为npm
  • 输入npm in,按下tab:自动提示可选子命令 init、install
  • 输入npm inst,按下tab:自动补全为 npm install
  1. const readline = require('readline');
  2. const fs = require('fs');
  3. function completer(line) {
  4. const command = 'npm';
  5. const subCommands = ['help', 'init', 'install'];
  6. // 输入为空,或者为npm的一部分,则tab补全为npm
  7. if(line.length < command.length){
  8. return [command.indexOf(line) === 0 ? [command] : [], line];
  9. }
  10. // 输入 npm,tab提示 help init install
  11. // 输入 npm in,tab提示 init install
  12. let hits = subCommands.filter(function(subCommand){
  13. const lineTrippedCommand = line.replace(command, '').trim();
  14. return lineTrippedCommand && subCommand.indexOf( lineTrippedCommand ) === 0;
  15. })
  16. if(hits.length === 1){
  17. hits = hits.map(function(hit){
  18. return [command, hit].join(' ');
  19. });
  20. }
  21. return [hits.length ? hits : subCommands, line];
  22. }
  23. const rl = readline.createInterface({
  24. input: process.stdin,
  25. output: process.stdout,
  26. completer: completer
  27. });
  28. rl.prompt();

代码运行效果如下,当输入npm in,按下tab键,则会自动提示可选子命令init、install。

  1. autoComplete git:(master) node app.js
  2. > npm in
  3. init install

例子:命令行工具:npmt init

下面借助readline实现一个迷你版的npm init功能,运行脚本时,会依次要求用户输入name、version、author属性(其他略过)。

这里用到的是rl.question(msg, cbk)这个方法,它会在控制台输入一行提示,当用户完成输入,敲击回车,cbk就会被调用,并把用户输入作为参数传入。

  1. const readline = require('readline');
  2. const fs = require('fs');
  3. const rl = readline.createInterface({
  4. input: process.stdin,
  5. output: process.stdout,
  6. prompt: 'OHAI> '
  7. });
  8. const preHint = `
  9. This utility will walk you through creating a package.json file.
  10. It only covers the most common items, and tries to guess sensible defaults.
  11. See \`npm help json\` for definitive documentation on these fields
  12. and exactly what they do.
  13. Use \`npm install <pkg> --save\` afterwards to install a package and
  14. save it as a dependency in the package.json file.
  15. Press ^C at any time to quit.
  16. `;
  17. console.log(preHint);
  18. // 问题
  19. let questions = [ 'name', 'version', 'author'];
  20. // 默认答案
  21. let defaultAnswers = [ 'name', '1.0.0', 'none' ];
  22. // 用户答案
  23. let answers = [];
  24. let index = 0;
  25. function createPackageJson(){
  26. var map = {};
  27. questions.forEach(function(question, index){
  28. map[question] = answers[index];
  29. });
  30. fs.writeFileSync('./package.json', JSON.stringify(map, null, 4));
  31. }
  32. function runQuestionLoop() {
  33. if(index === questions.length) {
  34. createPackageJson();
  35. rl.close();
  36. return;
  37. }
  38. let defaultAnswer = defaultAnswers[index];
  39. let question = questions[index] + ': (' + defaultAnswer +') ';
  40. rl.question(question, function(answer){
  41. answers.push(answer || defaultAnswer);
  42. index++;
  43. runQuestionLoop();
  44. });
  45. }
  46. runQuestionLoop();

运行效果如下,最后还像模像样的生成了package.json(害羞脸)。

  1. commandLine git:(master) node app.js
  2. This utility will walk you through creating a package.json file.
  3. It only covers the most common items, and tries to guess sensible defaults.
  4. See `npm help json` for definitive documentation on these fields
  5. and exactly what they do.
  6. Use `npm install <pkg> --save` afterwards to install a package and
  7. save it as a dependency in the package.json file.
  8. Press ^C at any time to quit.
  9. name: (name) hello
  10. version: (1.0.0) 0.0.1
  11. author: (none) chyingp

写在后面

有不少基于readline的有趣的工具,比如各种脚手架工具。限于篇幅不展开,感兴趣的同学可以研究下。

相关链接

https://nodejs.org/api/readline.html