Node.js learning 6 (readline)

1, What is readline

readline is node JS implements the encapsulated module of standard input and output. Through this module, we can read the data stream line by line.
The readline module provides an interface for reading data from a readable stream (such as process.stdin) one line at a time. You can access it in the following ways:

const readline = require('readline');

2, How to use readline

Create readline instance
Call related interface methods
Listening and processing readline events
Examples are as follows:

//Introducing readline module
const readline = require('readline');
//Create readline interface instance
let r1 = readline.createInterface({
    input:process.stdin,
    output:process.stdout
});
//Using the question method
r1.question('what do you want to eat?',function (anwser){
    console.log(`I would like to eat ${anwser}`);
    //Add the close event, otherwise it will not end
    r1.close();
});
//close event listening
r1.on('close',function (){
    //End program
    process.exit(0);
});

The results are as follows:

Step 1: createInterface creates an interface instance
Step 2: call relevant methods, such as question input method
Step 3: listen for the close event of readline
be careful:
(1) In the createInterface, you need to pass in standard input and output as the input and output stream of data;
(2) In the callback function of the question method, the user's input can be obtained and processed. At the same time, the close operation is performed to end the program, otherwise the program will not end;
(3) In the listening of the close event, the process is executed Exit (0) to exit the program, because the readline module will not end as long as it obtains user input at the beginning. This direct way must be used to end the program.

1. Interface class

Inherited from: < EventEmitter >

      readline. An instance of the interface class uses readLine Constructed by the createinterface () method. Each instance is associated with a single input readable stream and a single output writable stream. The output stream is used to print prompts for user input that arrives and is read from the input stream.

2. close event

The close event is triggered when one of the following conditions occurs:
(1)rl. The close () method is called, readLine The interface instance gives up control over the input and output streams;
(2) The input stream receives its' end 'event;
(3) The input stream receives Ctrl+D to signal the end of transmission (EOT);
(4) The input stream receives Ctrl+C to send a SIGINT signal, and is displayed in readLine 'sigint 'event listener is not registered on the interface instance.
The listener function is called without passing in any parameters.
Once the 'close' event is triggered, readLine The interface instance is complete.

3,readline.createInterface(options)

options < Object >
input <stream. Readable > the readable stream to listen on. This option is required.
output <stream. Writable > the writable stream to write the data read line by line.
Completer < function > optional function for tab auto completion.

3, Instances: input and output

Line event. This event is triggered after the user enters a line and presses enter. It will transfer the data entered by the user back through the callback function. The data entered by the user can be processed in this method.

//Introducing readline module
const readline = require('readline');
//Create readline interface instance
let r1 = readline.createInterface({
    input:process.stdin,
    output:process.stdout
});
//Register line events
r1.on('line',function (line){
    switch(line.trim()){
        case 'copy':
            console.log('copy');
            break;
        case 'hello':
            r1.write('write');
            console.log('world');
            break;
        case 'close':
            r1.close();
            break;
        default:
            console.log('Command not found');
            break;
    }
});
//close event listening
r1.on('close',function (){
    console.log('----end----');
    process.exit(0);
});

The function of trim() is to remove whitespace on both sides of a string

4, Example: analog command line input and output

const readline = require('readline');
const r1 = readline.createInterface({
    input:process.stdin,
    output:process.stdout
});
//Is to set a prompt test for each line
r1.setPrompt('test');
//The prompt method is waiting for the user to enter data
r1.prompt();
r1.on('line',function (line){
    switch(line.trim()){
        case 'copy':
            console.log('copy');
            break;
        case 'hello':
            console.log('world');
            break;
        case 'close':
            r1.close();
            break;
        default:
            console.log('Command not found');
            break;
    }
	//R1 here Prompt () listened for the 'line' event because the prompt method called
	//The data is read only once at a time, so it is called again in this method
	//prompt method, so that you can continue to read user input to achieve a command
	//Line effect.
    r1.prompt();
});
r1.on('close',function (){
    console.log('----end----');
    process.exit(0);
});

The method setPromat(promat) is to set a prompt for each line, just like > on the window command line, where test is set.

prompt() is the most important method, because it embodies the core role of readline. It reads data in behavioral units. prompt method is waiting for the user to input data.

The results are shown in the figure below:

Keywords: node.js Front-end Back-end

Added by bossman on Tue, 18 Jan 2022 01:36:40 +0200