简体   繁体   中英

NodeJS - standalone application to making user to input data

Does NodeJS has any functionality for accepting input via standard input from a user. In Browser based JS we used to use 'prompt' functionality for same but the this would not work on NodeJS standalone app.

node one.js

Enter any number:

<program accepts the number and does the processing>

As vinayr said, you should use readline. An example of what you desire:

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

I would like to suggest you to use stdio . It is a module that aims to simplify your life with standard input/output. One of its main features is the standard input reading, line by line, and it can be done as follows:

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: I'm the stdio creator. :-)

NPM

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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