Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input() in for Node.js

I'm coming to Node.js from Python 3 and wondering if Node.js has something that I can use that is basically the same as Python's input for example lets say we have this code:

def newUser(user = None, password = None):
    if not user: user = input("New user name: ")
    if not password: password = input("Password: ")
    return "Welcome, your user name is %s and your password is %s" % (user, password)

# Option one
>>> newUser(user = "someone", password = "myPassword") 
'Welcome your user name is someone and your password is myPassword'

# Option Two
>>> newUser()
New User name: someone
Password: myPassword
'Welcome your user name is someone and your password is myPassword'

Can node.js do the same thing? If so how? If you have any documentation on it that would be useful also so I can just refer back their if I have any further questions. My main problem though is node.js not waiting for me to submit my reply/answer to the question like python does.

like image 869
ruler Avatar asked Feb 02 '26 07:02

ruler


2 Answers

Readline module: http://nodejs.org/api/readline.html

Here is your example rewritten to Node.js:

var readline = require('readline');

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

rl.question("New user name:", function(user) {
    rl.question("New password:", function(password) {
        var newUser = new User(user, password);
        // do something...
        rl.close();
    }
});

It looks a little bit different, because the console uses non-blocking IO (like the rest of Node.js and unlike Python).

like image 185
Martin Majer Avatar answered Feb 04 '26 21:02

Martin Majer


You can use prompt to read input from user.

Sample code:

var util = require('util');
var prompt = require('prompt');

// start the prompt
prompt.start();

// text that appears on each prompt
// prompt.message = 'Enter';
// prompt.delimiter = ' → ';

// schema to take user input
var schema = {
  properties: {
    username: {
      message: 'Username',
      required: true
    },

    password: {
      name: "Password",
      required: true
    }
  }
};

function newUser(username, password, callback) {
  if (typeof username === 'function') {
    callback = username;
    username = null;
    password = null;
  }

  var respond = function (err, newuser) {
    callback(null, util.format('Welcome, your user name is %s and your password is %s', newuser.username, newuser.password));
  }
  if (!username && !password) {
    prompt.get(schema, respond);
  } else {
    var newuser = {
      username: username,
      password: password
    };

    respond(null, newuser);
  }
}

/** Test Code --------------------------------------------------------------- */
if (require.main === module) {
  (function () {
    var logcb = function (err, res) {
      console.log(err || res);
    }

    // new user with username and password
    newUser('hello', 'password', function (err, res) {
      logcb(err, res);

      // new user with prompt
      newUser(logcb);
    });
  })();
}

Hope this helps, you can make a library routine out of this and use it every time you need to.

like image 39
vmx Avatar answered Feb 04 '26 21:02

vmx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!