2013-06-01 55 views
0

在Ruby中,我們可以做這樣的如何要求在命令行中問題的NodeJS

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中?

+0

http://nodejs.org/api/readline.html – jgb

回答

5

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(); 
}); 
+0

注意,這需要節點'> = 0.10' – Mustafa

+1

,如果我想要什麼問多個問題我怎麼能做到這一點? –

+0

@NwawelAIroume https://stackoverflow.com/a/8129748/894402 –