Node.js - How to read user input from the console?

0

In Java:

System.out.println("Diga algo: ");
Scanner leitor = new Scanner(System.in);
String resp = leitor.nextLine(); //ou nextInt, nextDouble, etc

In C ++

cout << "Diga algo: ";
string name;
cin >> name;

How to read input from console in Node.js?

    
asked by anonymous 26.03.2018 / 06:19

2 answers

1

Using the readline module:

var readline = require('readline');
var resp = "";

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

leitor.question("Qual módulo pra ler dados no node.js?\n", function(answer) {
    var resp = answer;
    console.log("\nSua resposta '" + resp + "' foi grava com sucesso numa variável inútil");
    leitor.close();
});
    
26.03.2018 / 06:19
1

    
process.stdin.on('readable', ()=>{ 
  // reads what is being typed. 
  let variable = process.stdin.read(); 
  // trying to read 
  variable = variable.toString().replace(/\n/, ""); 
  variable = variable.replace(/\r/, ""); });

// Hope this helps
    
13.12.2018 / 18:49