Accepting input from console in Node.Js

In this post, we will demonstrate how to accept input and perform operation in node.js from a console. Node.js is an open-source, cross-platform runtime environment for developing server-side web applications. Node.js applications are written in JavaScript and can be run within the Node.js runtime on OS X, Microsoft Windows, Linux, FreeBSD, NonStop, IBM AIX, IBM System z and IBM i.

Screenshot:

The Code

var readline = require('readline');

var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

rl.question("What is your name ? ", function(answer) {
// TODO: Log the answer in a database
console.log("Hello "+answer+" how are u ?");
rl.close();
});

Got any doubts, just go ahead and comment them ! 🙂

Bikash Panda
Catch Me On

Bikash Panda

Blogger / Embedded System Developer at WeArGenius
A techie, tinkerer and tech lover, who loves to blog and feels everyone can learn tech provided they have the right attitude towards learning and passion. By profession, I am an IOT developer working in Smart Home/ Smart Grid domain.
Bikash Panda
Catch Me On

Related posts