NodeJSには、ユーザーからの標準入力を介して入力を受け入れる機能がありますか?ブラウザベースのJSでは、以前は「プロンプト」機能を使用していましたが、これはNodeJSスタンドアロンアプリでは機能しませんでした。
node one.js
Enter any number:
<program accepts the number and does the processing>
NodeJSには、ユーザーからの標準入力を介して入力を受け入れる機能がありますか?ブラウザベースのJSでは、以前は「プロンプト」機能を使用していましたが、これはNodeJSスタンドアロンアプリでは機能しませんでした。
node one.js
Enter any number:
<program accepts the number and does the processing>
vinayrが言ったように、readlineを使用する必要があります。あなたが望むものの例:
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();
});
readlineプロンプトを使用するhttp://nodejs.org/api/readline.html
stdioの使用をお勧めします。標準の入出力で生活を簡素化することを目的としたモジュールです。その主な機能の1つは、行ごとの標準入力読み取りであり、次のように実行できます。
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
クリエーターです。:-)
(出典:nodei.co)