Node.js uses ReadLine module to realize input and output _node.js

Source: Internet
Author: User
Tags event listener readline stdin trim

What is ReadLine

ReadLine is a packaged module that implements standard input and output in Node.js, through which we can read data streams in rows. Use require ("ReadLine") to reference a module.

How to use ReadLine

In terms of use, learning readline, we need to learn its three parts:

    1. Create a ReadLine instance
    2. Learning the interface method inside
    3. Learning to monitor and handle ReadLine events

Here are some examples to learn about these three parts.

Example 1: My name is Xiao Ming

The code is as follows:

/**
 * Created by the Administrator on 2015/9/10
 . *
///Introduce ReadLine module
var readline = require (' ReadLine ');

Create ReadLine Interface instance
var RL = Readline.createinterface ({ 
 Input:process.stdin,
 output:process.stdout
});

Question Method
rl.question ("What's your name?") ", function (answer) { 
 Console.log (" name is: "+answer);
 No close, no end
 rl.close ();
});
the Close event listens for Rl.on ("Closed", function () { 
 //End program
 process.exit (0);
});

The above example uses the three parts that we need to learn, first using the createInterface create an interface instance, then using the question method to ask the name, and finally the listening readline close event, because both the method name and the event's listener name are more intuitive, About their role can also be at a glance, I have only mentioned three points to note:

    1. In createInterface , we need to pass in the standard input output as the input and output stream of the data.
    2. In the question callback function of the method, we can get the input of the user and process it, and we do the close operation to end the program, otherwise the program will not end.
    3. In the close event listener, we performed the process.exit(0) operation to get the program to quit because the readline module would not end up getting user input at first, and must end the program in a direct way

Example 2: Input and output

/**
 * Created by the Administrator on 2015/9/10
 . *
///Introduce ReadLine module
var readline = require (' ReadLine ');

The var RL = Readline.createinterface ({ 
 Input:process.stdin,
 output:process.stdout
});

Rl.on (' line ', function (line) { 
 switch (Line.trim ()) {case
  ' copy ':
   console.log ("copy");
   break;
  Case ' Hello ':
   rl.write ("write");
   Console.log (' world! ');
   break;
  Case ' close ':
   rl.close ();
   break;
  Default:
   console.log (' No command found! ');
   break;
 }
});
Rl.on (' Close ', function () { 
 console.log (' Bye Bye ');
 Process.exit (0);
});

The ' line ' event, which is the event that triggers when a user loses a row, presses a carriage return, and it passes the data entered by the user back through the callback function, where the data entered by the user can be processed.

Example 3: Input and output similar to command line

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 ("copy");
   break;
  Case ' Hello ':
   console.log (' world! ');
   break;
  Case ' close ':
   rl.close ();
   break;
  Default:
   console.log (' No command found! ');
   break;
 Rl.prompt ();
});

Rl.on (' Close ', function () { 
 console.log (' Bye bye! ');
 Process.exit (0);
});

Run the screenshot as follows:

There are two new methods in this example

    1. Method is setPromat(promat) to set a prompt for each line, like the > of Window command line, where we set up the test>
    2. promat()Can be considered the most important method, because it embodies the Readline core role in the behavior unit to read data, the premat way is waiting for user input data
    3. This also listens to the ' line ' event, because the Promat method only reads the data once at a time, so a method is called again in this method, so that promat you can continue to read the user input to achieve a command line effect

Appendix

Here are only three simple examples to illustrate Readline the use of, if you want to learn more about Readline the usage and more methods and events, you can see the official API

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.