繁体   English   中英

NodeJS-使用户输入数据的独立应用程序

[英]NodeJS - standalone application to making user to input data

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

我建议您使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM