2012-10-05 31 views
0

NodeJS是否有任何功能可以接受來自用戶的標準輸入的輸入。在基於瀏覽器的JS中,我們曾使用「提示」功能,但這不適用於NodeJS獨立應用程序。NodeJS - 讓用戶輸入數據的獨立應用程序

node one.js 

Enter any number: 

<program accepts the number and does the processing> 

回答

0

使用輸入行提示符http://nodejs.org/api/readline.html

+1

嗨vinayr,而此鏈接可能會有所幫助,堆棧溢出的目的是爲了知識的資源,幾年來。如果這個鏈接永遠不會中斷,你的回答將毫無用處。爲了確保您的帖子能夠在未來數年內保持有效,請使用[編輯]鏈接爲帖子正文添加示例,並將該鏈接留作有用的參考,並確保鏈接不會破壞您的答案。祝你好運! – jmort253

1

正如vinayr說,你應該使用輸入行。一個你想要的例子:

var readline = require('readline'); 

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

rl.question("Input number:", function(numAnswer) { 
    // TODO: Log the answer in a database 
    var num = parseInt(numAnswer); 
    processingFunctionYouUse(num); 
    rl.close(); 
}); 
0

我想建議你使用stdio。這是一個旨在通過標準輸入/輸出來簡化您的生活的模塊。它的一個主要特點是讀取標準輸入,一行行,它可以如下進行:

var stdio = require('stdio'); 
stdio.readByLines(function (line) { 
    // This function is called for every line while they are being read 
}, function (err) { 
    // This function is called when the whole input has been processed 
}); 

PD:我是stdio創造者。 :-)

NPM