3

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.

4 Answers 4

6

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).

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

1

Readline module definitely does the job, as already mentioned. But to get something that looks like input() in Python (and avoid numerous callbacks), you can promisify question method:

const readline = require('node:readline');
const util = require('node:util');

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

const question = util.promisify(rl.question).bind(rl);

async function prompt() {
  
    const username = await question('New user name: ');
    const password = await question('New password: ');

    return new User(username, password);
}

Comments

0

I think that the kbd module (https://npmjs.org/package/kbd) is what you need.

This is a little C++ add-on module that make synchronous reading on keyboard.

1 Comment

you have to explain what that link contains and how that helps . reference can be given after the explaination

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.