2013-04-07 67 views
0

我写此代码使用从用户获取输入:我应该需要什么节点JS标准输入和标准输出

label Name: 
input(in); 

label LastName: 
input(in); 

process.stdin.resume(); 
process.stdin.setEncoding('utf8'); 

process.stdin.on('data', function(chunk){ 
process.stdout.write('data: ' +chunk); 
}); 

process.stdin.on('end', function(){ 
process.stdout.write('end'); 
}); 

和我保持收到此错误“语法错误:意外令牌)”。 ,我不明白为什么。 你能帮助我吗? 谢谢!

+0

在没有语法错误发生什么线的更复杂的情况的例子吗? – gmaliar 2013-04-07 09:11:09

回答

0

我不确定接收到的语法错误,但对于来自命令行的用户输入,nodejitsu的prompt模块非常棒。

var prompt = require('prompt'); 

// 
// Start the prompt 
// 
prompt.start(); 

// 
// Get two properties from the user: username and email 
// 
prompt.get(['firstname', 'lastname'], function (err, result) { 
    // 
    // Log the results. 
    // 
    console.log('Command-line input received:'); 
    console.log(' firstname: ' + result.firstname); 
    console.log(' lastname: ' + result.lastname); 
}); 

提示GitHub的页面有很多涉及验证等