Thursday, August 23, 2012

Get user input using nodejs

I found a way how to get user input using nodejs (to differentiate from client-side js, I will refer to javascript server-side as nodejs)--
$ node test.js
prompt: username:  jim
prompt: email:  jim@email.com
Command-line input received:
  username: jim
  email: jim@email.com

Here's the script for test.js--
var prompt = require('prompt')

prompt.start
prompt.get(['username', 'email'], function (err, result) {
  console.log('Command-line input received:')
  console.log('  username: ' + result.username)
  console.log('  email: ' + result.email)
})

I read from here that using semi-colons are optional. Notice that I wrote the script above to test if I'd get an error. I ran this script on nodejs CLI, and I did not get any error.

I am thinking that I can write statements, as long as I do one statement per line. I'll test this in subsequent nodejs scripts from here on. (*EDIT... I am not totally going to not use the semi-colon altogether.I am going to test what will happen when I play around with the script.)

To learn more about the NPM package "Prompt", click here.

No comments:

Post a Comment