Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use readline Module to realize Terminal input in Node.js

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian for you to introduce in detail "how to use the readline module to achieve terminal input in Node.js", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use the readline module to achieve terminal input in Node.js" can help you solve your doubts.

1 Overview

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

To use readline:

1. Introduce: require ('readline')

2. Create readline object (API)

3. Call the relevant methods of the interface

4. Monitor and handle readline events

Sample code:

/ / introduce readline module const readline = require ("readline"); / / create readline interface instance let R1 = readline.createInterface ({input: process.stdin, output: process.stdout}) / / call interface method r1.question ("what's your name\ t", function (answer) {console.log ("my name is:", answer); / / without close, r1.close () will not end }) / / close event listener r1.on ("close", function () {/ / Terminator process.exit (0);})

2 use of readline

Step 1: createInterface creates an interface instance

Step 2: call related methods, such as question method input

Step 3: listen to the close event of readline

Note:

1. In createInterface, you need to input 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, and the close operation is performed to end the program, otherwise the program will not end.

3. In the monitoring of close events, process.exit (0) is executed to make the program exit, because the readline module does not end as long as it starts to obtain user input, so it must use this direct way to end the program.

3 example 1: input and output

Sample code:

/ / introduce readline module let readline = require ("readline"); / / create an interface instance let R1 = readline.createInterface ({input: process.stdin, output: process.stdout}) / / call the interface method r1.on ("line", function (line) {switch (line.trim ()) {case "copy": console.log ("copy"); break Case "hello": r1.write ("Hello"); console.log ("World!"); break; case "close": r1.close (); break; default: console.log ("Command not found!") ; break;}) / / close event listens to r1.on ("close", function () {console.log (goodbye); process.exit (0);})

Note: 'line' event, which is the event that will be triggered after the user has typed a line and pressed enter. It will send the data entered by the user back through the callback function, and the data entered by the user can be processed in this method.

4 example 2: simulate the input and output of the command line

Sample code:

/ / introduce the readline module let readline = require ("readline"); / / create an interface instance let R1 = readline.createInterface ({input: process.stdin, output: process.stdout}) / / method method setPromat (promat), which sets a prompt for each line, / / just like window command line >, where Test > r1.setPrompt ("Test >") is set. / / prompt () is the most important method, because it embodies the core role of readline, / / reads data in behavioral units, and the prompt method is waiting for the user to enter data r1.prompt () / / calls the interface method / / listens for the 'line' event, because the prompt method is called only once / / so the prompt method is called again in this method In this way, you can continue to read user input / / to achieve a command-line effect 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.prompt ();}); / / close event listener r1.on ("close", function () {console.log (goodbye); process.exit (0);})

Description:

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

2. Prompt () is the most important method, because it embodies the core function of readline, reading data in behavioral units, and the prompt method is waiting for users to enter data.

3. The 'line' event is listened to, because the prompt method is called only once, so the prompt method is called again in this method, so you can continue to read the user input, thus achieving a command line effect.

After reading this, the article "how to use the readline module to achieve terminal input in Node.js" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report