如何在命令行中的nodejs中提问

时间:2013-06-01 11:12:37

标签: javascript node.js command-line-interface

在ruby中我们可以这样做

print "How old are you? "
age = gets.chomp()
print "How tall are you? "
height = gets.chomp()
print "How much do you weigh? "
weight = gets.chomp()

puts "So, you're #{age} years old, #{height} tall and #{weight} heavy."

它会喜欢这个

enter image description here

但如何在nodejs中执行此操作?

1 个答案:

答案 0 :(得分:7)

取自readline的node.js文档:

var readline = require('readline');

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

rl.question("What do you think of node.js? ", function(answer) {
  console.log("Thank you for your valuable feedback:", answer);

  rl.close();
});