Node.js | Readline() Module

Readline Module in Node.js allows the reading of input stream line by line. This module wraps up the process standard output and process standard input objects. Readline module makes it easier for input and reading the output given by the user. To use this module, create a new JavaScript file and write the following code at the starting of the application – 
 

var readline = require('readline');

The Readline module comes with different methods to interact with the user.
Interaction with the user: For the interaction, we will first create an interface for the input and output. For creating an interface, write the following code – 
 

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

var readline = require('readline');
 
var rl = readline.createInterface(
     process.stdin, process.stdout);

chevron_right


Here, the createInterface() method takes two arguments. The first argument will be for the standard input and the second one will be for reading the standard output. 
 

javascript



filter_none

edit
close

play_arrow

link
brightness_4
code

rl.question('What is your age? ', (age) => {
    console.log('Your age is: ' + age);
});

chevron_right


Here, rl.question() method is used for asking questions from the user and reading their reply (output). The first parameter is used to ask the question and the second parameter is a callback function which will take the output from the user as the parameter and return it on the console. 
The output for the above code will be – 
 

What is your age? 20
Your age is: 20

Here, the problem is it will not exit the application and it will keep asking for the inputs. To resolve this issue, rl.close() method is used. This method will close the interface. To use it in the application, write the following –
 

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

rl.question('What is your age? ', (age) => {
    console.log('Your age is: ' + age);
    rl.close();
});

chevron_right


We can also use a setPrompt() method is used to set the particular statement to the console. prompt() method for displaying the statement which is set in setPrompt() Method. Therefore, write the following code –
 

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

var readline = require('readline');
 
var rl = readline.createInterface(
        process.stdin, process.stdout);
 
rl.setPrompt(`What is your age? `);
rl.prompt()

chevron_right


This code will take input from the user. Now, we need a listener for reading the input from the user and displaying it to the console. For this purpose, Readline Module has a listener method called on that will take two parameters. The first parameter will the event and the second parameter will be a callback function that will return the output to the console. For example – 
 

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

var readline = require('readline');
 
var rl = readline.createInterface(
        process.stdin, process.stdout);
 
rl.setPrompt(`What is your age? `);
rl.prompt();
rl.on('line', (age) => {
    console.log(`Age received by the user: ${age}`);
    rl.close();
});

chevron_right


Here, rl.on() method takes the first argument as line event. This event is invoked whenever the user presse Enter key. The output for the above code will be – 
 



What is your age? 20
Age received by the user: 20

Events: Along with the listener, readline module also comes with the events properties. Let us learn about the various events. 
 

  • close: This event is invoked when either rl.close() method is called or when the user presses ctrl + c to close the interface.
  • line: This event is invoked whenever the user presses Enter or return keys. This event is called in the listener function. For example – 
     

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

rl.on('line', (input) => {
    console.log(`Input Received: ${input}`);
    rl.close();
});

chevron_right


  • pause: This event is invoked when the input stream is paused. For example – 
     

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

rl.on('pause', () => {
    console.log('Paused Event is invoked');
});

chevron_right


  • The output for the above code will be – 
     
Paused Event is invoked
  • resume: This event is invoked whenever the input is resumed. For example – 
     

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

rl.on('resume', () => {
        console.log('Resume Event is invoked.');
});

chevron_right


  • SIGINT: This event is invoked whenever the user press ctrl + c button. If there is no event listeners registered when the SIGINT is invoked, then pause event will be invoked. For example – 
     

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

rl.on('SIGINT', () => {
    rl.question('Exit (y or n)? ', (input) => {
      if (input.match(/^y(es)?$/i)) { rl.pause(); }
    });
});

chevron_right


  • This code will ask the given question if ctrl + c key is pressed by the user. Here, if the provided input will match with y or yes, then the pause() method will be called on the interface.
  • SIGTSTP: This event is invoked whenever user gives ctrl + z input. This input is known as SIGTSTP. If a user does not provide the SIGTSTP input, then the current running process will be sent to the background. For example – 
     

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

rl.on('SIGTSTP', () => {
    console.log('SIGTSTP event is invoked.');
})

chevron_right


  •  
  • SIGCONT: This event is invoked when a process that was sent to background using SIGTSTP is again brought back to the front running process. For example – 
     

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

rl.on('SIGCONT', () => {
     console.log('SIGCONT event is invoked.');
     rl.prompt();
})

chevron_right


Conclusion: In this artile, we learned about the Readline Module in Node.js. Also, we learned about its various implementations using methods, variables, events, and listeners.
 




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : zack_aayush

Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.