Node.js
Readline
Buscar..
Sintaxis
- const readline = require ('readline')
- readline.close ()
- readline.pause ()
- readline.prompt ([preserveCursor])
- readline.question (consulta, devolución de llamada)
- readline.resume ()
- readline.setPrompt (indicador)
- readline.write (datos [, clave])
- readline.clearLine (stream, dir)
- readline.clearScreenDown (secuencia)
- readline.createInterface (opciones)
- readline.cursorTo (secuencia, x, y)
- readline.emitKeypressEvents (flujo [, interfaz])
- readline.moveCursor (secuencia, dx, dy)
Lectura de archivos línea por línea
const fs = require('fs');
const readline = require('readline');
const rl = readline.createInterface({
input: fs.createReadStream('text.txt')
});
// Each new line emits an event - every time the stream receives \r, \n, or \r\n
rl.on('line', (line) => {
console.log(line);
});
rl.on('close', () => {
console.log('Done reading file');
});
Solicitar la entrada del usuario a través de CLI
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('What is your name?', (name) => {
console.log(`Hello ${name}!`);
rl.close();
});
Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow