Learn About Console Application In Node.js

This article will go through the following points,

  • Prerequisite
  • Install Node.js Modules Intellisense extension in Visual Studio Code
  • What is Node.js Modules Intellisense extension in Visual Studio Code?
  • What is Package-lock.json?
  • Install ReadLine Package
  • What is ReadLine Package?
  • Console Application Overview
  • Console Application code and output.

Prerequisite

Software / Program Required

  1. Node JS
  2. Visual Studio Code

Packages Required

  1. Readline 

Install Node.js Modules Intellisense extension in Visual Studio Code

Node.js

What is Node.js Modules Intellisense extension in Visual Studio Code?

This extension will provide intellisense of node js while writing code in Visual Studio code.

What is Package-lock.json?

Package-lock.json file automatically gets generated during any modification/update in node_modules. You can check in this file for packages already installed.

For more details visit the following link:

https://docs.npmjs.com/files/package-lock.json

Node.js

Install ReadLine Package

If the READLINE package is not installed than you can install it with following command.

COMMAND:   npm install readline

What is ReadLine Package?

ReadLine package module provides an interface for reading data from a Readable stream.

To learn more about ReadLine package please visit the following link:

https://nodejs.org/api/readline.html#readline_readline

Console Application Overview

In this article we are going to create a console application in NODE JS.

Prompt the user to enter following fields data

  1. Enter Your Name
  2. Enter Your Age
  3. Enter Your Email ID
  4. Enter Your Mobile No.

Rules, Logic and Condition in Entry

  1. If Age is less than < 18 it will return and display the message:

    "Minimum required 18 years and you age is 16 , You should wait at least 2 year(s) more."
  1. If Age is greater than > 18 it will return and display the message:

“Great <UserName>   you can signin.”

“User Name : <UserName>”

“Age        : <UserAge> “

“Email ID   : <UserEmailID>”

“Mobile     : <UserMobile>”

NodeJS to run javascript code on server side.

Console Application Code

  1. var rl = require("readline");  
  2. var prompts = rl.createInterface(process.stdin, process.stdout);  
  3. prompts.question("Enter Your Name? "function(username) {  
  4.     prompts.question("Enter Your Age ? "function(userage) {  
  5.         prompts.question("Enter Your Email ID? "function(emailid) {  
  6.             prompts.question("Enter Your Mobile No.? "function(mobile) {  
  7.                 var message = "";  
  8.                 if (userage > 18) {  
  9.                     message = "\n\n Great! " + username + "\n\n You can signin." + "\n\n User Name : " + username + "\n\n Age       : " + userage + "\n\n Email ID  : " + emailid + "\n\n Mobile    : " + mobile;  
  10.                 } else {  
  11.                     message = "Minimum required 18 years and you age is " + userage + " , You should wait at least " + (18 - userage) + " year(s) more.";  
  12.                 }  
  13.                 console.log(message);  
  14.                 process.exit();  
  15.             });  
  16.         });  
  17.     });  
  18. }); 

OUTPUT

In output section I have given two examples.

In the first example the age is 9 years and in the second example the age is 20 years.

First Example

Node.js

Second Example

Node.js

Happy Coding…


Similar Articles