Home  >  Article  >  Web Front-end  >  Node.js uses the readline module to implement input and output

Node.js uses the readline module to implement input and output

高洛峰
高洛峰Original
2016-12-26 09:51:501181browse

What is Readline

Readline is a packaged module that implements standard input and output in Node.js. Through this module, we can read the data stream line by line. Modules can be referenced using require("readline").

How to use Readline

From the perspective of usage, to learn Readline, we need to learn three parts of it:

Create a Readline instance

Learn the interface methods inside

Learn to monitor and handle Readline events

below We learn these three parts through examples.

Example 1: My name is Xiao Ming

The code is as follows:

/**
 * Created by Administrator on 2015/9/10.
 */
// 引入readline模块
var readline = require('readline');
 
//创建readline接口实例
var rl = readline.createInterface({ 
 input:process.stdin,
 output:process.stdout
});
 
// question方法
rl.question("你叫什么?",function(answer){ 
 console.log("名字是:"+answer);
 // 不加close,则不会结束
 rl.close();
});
 
// close事件监听
rl.on("close", function(){ 
 // 结束程序
 process.exit(0);
});

The above example uses the three parts we need to learn. First, createInterface is used to create an interface instance, and then the question method is used to ask for the name. The last thing is to monitor the close event of readline, because both the method name and the name of the event monitoring are relatively intuitive, and their functions can also be understood at a glance. I will only mention three points to note here:

In createInterface, we need Pass in standard input and output as the input and output stream of data

In the callback function of the question method, we can obtain the user's input and process it. At the same time, we perform a close operation to end the program, otherwise the program will not end

In the monitoring of the close event, we executed process.exit(0) to exit the program. Because the readline module will not end as soon as it starts to obtain user input, this direct method must be used to end the program

Example 2: Input and output

/**
 * Created by Administrator on 2015/9/10.
 */
// 引入readline模块
var readline = require('readline');
 
var rl = readline.createInterface({ 
 input: process.stdin,
 output: process.stdout
});
 
rl.on('line', function(line){ 
 switch(line.trim()) {
  case 'copy':
   console.log("复制");
   break;
  case 'hello':
   rl.write("Write");
   console.log('world!');
   break;
  case 'close':
   rl.close();
   break;
  default:
   console.log('没有找到命令!');
   break;
 }
});
rl.on('close', function() { 
 console.log('bye bye');
 process.exit(0);
});

'line' event, this event is triggered after the user finishes typing a line and presses Enter. It will pass the data entered by the user back through the callback function, which can be This method handles the data input by the user

Example 3: Command line-like input and output

var readline = require('readline'); 
var rl = readline.createInterface(process.stdin, process.stdout);
 
rl.setPrompt('Test> '); 
rl.prompt();
 
rl.on('line', function(line) { 
 switch(line.trim()) {
  case 'copy':
   console.log("复制");
   break;
  case 'hello':
   console.log('world!');
   break;
  case 'close':
   rl.close();
   break;
  default:
   console.log('没有找到命令!');
   break;
 }
 rl.prompt();
});
 
rl.on('close', function() { 
 console.log('bye bye!');
 process.exit(0);
});

The running screenshot is as follows:

Node.js uses the readline module to implement input and output

There are two new methods in this example

method setPromat(promat), which is Set a prompt for each line, just like the > of the window command line. What we set here is Test>

promat() can be regarded as the most important method, because it reflects the core function of Readline, in units of lines. To read data, the premat method is waiting for the user to input data

The 'line' event is monitored here again, because the promat method will only read the data once once, so the promat method is called again in this method, so You can continue to read user input to achieve a command line effect

Appendix

Here are only three relatively simple examples to illustrate the use of Readline. If you want to know more about Readline For usage and more methods and events, you can go to the official API

Summary

The above is the entire content of this article. I hope the content of this article can bring some help to everyone's study or work. If you have any questions You can leave messages to communicate.

For more articles related to Node.js using the readline module to implement input and output, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn