"Node. js" ReadLine (Progressive Read)

Source: Internet
Author: User
Tags stdin

Website Link: http://nodejs.cn/api/readline#readline_readline

The require(‘readline‘) module provides an interface for reading data from a readable stream (such as process.stdin ), reading one line at a time. It can be used in the following ways:

Const ReadLine = require (' ReadLine ');

Example, readline the basic usage of the module:

Const ReadLine = require (' readline '= readline.createinterface ({  Input:process.stdin,  output: Process.stdout}); Rl.question (' What do you think of the node. JS Chinese web? ', (answer) = {  //  to process the answer   console.log (' Thank you for your feedback: ${answer} ');  Rl.close ();});

Interface class

readline.InterfaceAn instance of a class is constructed using a readline.createInterface() method. Each instance is associated with a readable input stream and a writable output stream. outputThe stream is used to enter a print hint for the arriving user and read from the input stream.

' Line ' event

inputThe event is triggered whenever the stream receives a line terminator ( \n , \r or \r\n ) ‘line‘ . Typically occurs when the user presses the <Enter> key or <Return> key. The listener function is called with a string containing the input of the received line.

Rl.on (' line ', (input) = {  Console.log (' Received: ${input} ');});
Rl.question (query, callback)
Rl.question (' What's your favorite food? ', (answer) = {  console.log (' Your favorite food is ${answer} ') ;
Example: reading a file stream line by row

Consumes input one line at a time from a file system readable stream:

Const ReadLine = require (' readline '= require (' fs '= readline.createinterface ({  input: Fs.createreadstream (' sample.txt ')}); Rl.on (' line ', (lines) = = {  console.log (' single-row content of file: ${ Line} ') ;

Using the ReadLine module to implement node. js input and output

Reference: http://blog.csdn.net/zgljl2012/article/details/48321171

Create ReadLine instances, learn the interface methods inside, learn to listen and handle ReadLine events

Example 1: My name is Xiao Ming
//Introducing the ReadLine modulevarReadLine = require (' ReadLine '));//Creating an ReadLine interface instancevarRL =Readline.createinterface ({input:process.stdin, output:process.stdout});//Question MethodRl.question ("What's your name?") ",function(Answer) {Console.log ("The name is:" +answer); //without close, it will not endrl.close ();});//Close Event ListenerRl.on ("Close",function(){   //End ProgramProcess.exit (0);});

First, we used the Createinterface to create an interface instance, then used the question method to ask for names, and finally the close event to listen for ReadLine, because both the method name and the event's listener name are more intuitive, the role of them can be at a glance, I'm just going to mention three points here. Note:

1) in Createinterface, we need to pass in the standard input and output as the input and output stream of the data

2) in the callback function of the question method, we can get the user's input and processing, and we do a close operation to end the program, otherwise the program will not end

3) in the Close event listener, we execute process.exit (0) to make the program exit, because the ReadLine module will not end as long as the user input is obtained at the beginning, and must be used in this direct way to end the program

Example 2: Input and output
//Introducing the ReadLine modulevarReadLine = require (' ReadLine '));varRL =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 is an event that fires when a user has finished a row and presses a carriage return , which returns the data entered by the user through a callback function, which can be used to process user input data.

"Node. js" ReadLine (Progressive Read)

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.