admin管理员组

文章数量:1289586

I'm ing 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 "Wele, your user name is %s and your password is %s" % (user, password)

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

# Option Two
>>> newUser()
New User name: someone
Password: myPassword
'Wele 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.

I'm ing 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 "Wele, your user name is %s and your password is %s" % (user, password)

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

# Option Two
>>> newUser()
New User name: someone
Password: myPassword
'Wele 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.

Share Improve this question edited Apr 21, 2015 at 12:36 gotofritz 3,3811 gold badge32 silver badges49 bronze badges asked Dec 8, 2013 at 22:20 rulerruler 6231 gold badge10 silver badges17 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

Readline module: http://nodejs/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).

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('Wele, 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.

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);
}

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

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

本文标签: javascriptInput() in for NodejsStack Overflow