2013-04-07 71 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的頁面有很多涉及驗證等