| by Arround The Web | No comments

How Does the “createInterface()” Work in Node.js?

Node.js is a well-known open-source JavaScript runtime environment that helps in generating dynamic and highly scalable web applications. It comes with a wide range of built-in modules to fulfill the specified task based on their names and functionalities. It is such that the “readline” module reads the input stream and gives the resultant output. Furthermore, this module also contains several methods that perform special functionalities such as “createInterface()” creates a readline interface, “cursorTo()” moves the cursor, “clearLine()” clears the line, and much more.

This guide will elaborate on the working of the “createInterface()” in Node.js.

How Does the “createInterface()” Work in Node.js?

The “createInterface()” is the built-in method of the “readline” module that takes the user input and provides the output stream to create a readline interface. Its working relies on its basic syntax which is written below:

Syntax

readline.createInterface(input, output, completer)

 
According to the above syntax, the “createInterface()” method supports the following three parameter:

    • input: It denotes the input stream that uses the “process.stdin” property to take the standard input from the user through CLI(command line).
    • output: It represents the output stream that applies the “process.stdout” to print the information taken as input by the user.
    • completer: It is an optional parameter that is utilized for autocompletion. Its value is “NULL” by default.

Return Value: The “createInterface()” method returns nothing as it only creates a readline interface.

Now, use the above-defined method practically.

Example: Applying the “createInterface()” Method to Create a Readline Interface

This example shows the practical implementation of the “createInterface()” method for creating a readline interface with the help of the following code lines:

const readline = require('readline')
const rl = readline.createInterface({
 input: process.stdin,
 output: process.stdout
})
rl.question(`Best platform for technical content? `, website => {
rl.question(`Which category you would like to explore? `, category => {
 console.log(`Website: ${website}, Category: ${category}`)
 rl.close()
 })
})

 In the above lines of code:

    • Firstly, the “require()” method imports the “readline” module in the current Node.js project.
    • Next, the “createInterface()” method specifies the “input” and “output” streams as an object. The “input” stream uses the “process.stdin” property for taking input from the user.
    • The “output” stream utilizes the “process.stdout” property for reading the input stream and printing it as the standard output of the given input stream.
    • After that, the “rl.question()” method takes the input from the user. It specifies the question as the first and the callback function as its second argument. The given callback arrow function retrieves the user-entered values.
    • In the definition of the given “website”, and the “category” callback arrow function, the “console.log()” method is used to display the entered values.
    • Lastly, the “rl.close()” method closes the above-created interface.

Note: Create a “.js” file of any name and write the above code lines into it. For instance, we have created “index.js”.

Output

Initiate the “index.js” file by executing the provided command:

node index .js

 
The following output shows a readline interface that takes the user input from the command line and then displays the entered value as the standard output:


That’s all about the working of the “createInterface()” in Node.js.

Conclusion

The “createinterface()” method of the “readline” module works on the “input” and “output” stream for the creation of an interface. Both of the specified streams pass as the mandatory argument of the “createInterface()” method. These particular streams use the “process.stdin”, and the “process.stdout” properties of the “process” module to take the user input and then retrieve the entered value as an output. This guide has deeply explained the working of the “createInterface()” in Node.js.

Share Button

Source: linuxhint.com

Leave a Reply