In Node.js
you can use the readline
:
const readline = require('readline');
const input = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
input.question('Informe um número: ', (resposta) => {
// TODO: Log the answer in a database
console.log('Número informado: ${resposta}');
input.close();
});
If you want to use async/await
you can do it as follows:
const readline = require('readline');
const input = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const perguntar = (pergunta) => new Promise(resolver => input.question(pergunta, (resposta) => resolver(resposta)));
const executar = async () => {
console.time('Execução');
try {
const resposta = await perguntar('Informe um número: ');
console.log('Número informado: ${resposta}');
} catch (err) {
console.log(err)
}
// Totaliza o tempo de execução
console.timeEnd('Execução');
}
executar();
readline
The readline module provides an interface for reading data from a Readable stream (such as process.stdin) one line at a time.
Free translation:
The readline module provides an interface for reading data from a readable stream (like process.stdin) one line at a time.