nodejs

Node.js program that prompts the user for input and then prints it out to the console

In this post, I am going to write a simple Node.js program that prompts the user for input and then prints it out to the console:


Here’s what this code does:

  1. It uses Node.js’s built-in readline module to create an interface for reading user input and writing output.
  2. It creates an instance of the interface by calling readline.createInterface(), passing in process.stdin (which represents the standard input stream) as the input and process.stdout (which represents the standard output stream) as the output.
  3. It calls the rl.question() method to prompt the user for input. This method takes two arguments: the prompt to display to the user, and a callback function to run when the user enters a line of input.
  4. The callback function logs the user’s input to the console using console.log(), and then closes the interface using rl.close().

To run this program, save the code to a file (e.g. index.js) and run it with Node.js by typing node index.js in your terminal.

Related Posts

Leave a Reply