Tuesday, 2 October 2018

node.js: process.stdin: Read input

‘process.stdin’ returns a stream that connected to standard input (stdin).

HelloWorld.js
process.stdin.setEncoding('utf8');

console.log("Please Enter some information");

/*
 * Attaching data event listener, correspondent function is called, when user
 * entered some information
 */
process.stdin.on('data', (dataReadFromUser) => {
  console.log("User Entered '%s'", dataReadFromUser.trim());
  console.log("\nPlease Enter some information");
});

C:\Users\krishna\Documents\Study\nodejs\examples>node HelloWorld.js
Please Enter some information
Hello krishna
User Entered 'Hello krishna'

Please Enter some information
How Are You
User Entered 'How Are You'

Please Enter some information


Previous                                                 Next                                                 Home

No comments:

Post a Comment