. Advertisement .
..3..
. Advertisement .
..4..
Node.js readline is a module enabling the reading of input streams. It wraps the process standard input and output objects, making it easier for reading the output by the user.
Follow this post to find more details about this one and the best methods to use it.
What Is Node.js Readline?
Example of Node.js readline:
var readline = require('readline');
var fs = require('fs');
var myInterface = readline.createInterface({
input: fs.createReadStream('demofile1.html')
});
var lineno = 0;
myInterface.on('line', function (line) {
lineno++;
console.log('Line number ' + lineno + ': ' + line);
});
As mentioned above, the Readline module allows for an effective way to read a data stream line by line.
Syntax:
var readline = require('readline');
Create an interface:
var rl = readline.createInterface(
process.stdin, process.stdout);
This createInterface() method takes input and output parameters to create a readline interface. The third one works for autocompletion. For instance, you can access the tab to complete a given string.
How To Read Input Stream By Node.js Readline
Method 1: rl.question()
It asks questions from the users and then reads the reply. The first parameter aks the questions while the second one works as a callback function to take the output:
What is your age? 20
Your age is: 20
To exit the application, it is vital to use rl.close() to close the interface:
rl.question('What is your age? ', (age) => {
console.log('Your age is: ' + age);
rl.close();
});
Method 2: setPrompt()
Use this way to set the statement to the console. The method can be used to display the statement in setPrompt() method:
var readline = require('readline');
var rl = readline.createInterface(
process.stdin, process.stdout);
rl.setPrompt(`What is your age? `);
rl.prompt()
The code takes the user’s input and there will be a listener to read it. To do this, the Readline Module offers a listener method with two parameters. The first one takes the event and the second one calls it back.
var readline = require('readline');
var rl = readline.createInterface(
process.stdin, process.stdout);
rl.setPrompt(`What is your age? `);
rl.prompt();
rl.on('line', (age) => {
console.log(`Age received by the user: ${age}`);
rl.close();
});
Method 3: createInterface()
You can create an Interface object to read the input streams:
var readline = require('readline');
var rl = readline.createInterface(
process.stdin, process.stdout);
Method 4: cursorTo()
score: () => {
BEAST.debugging.report(`draw: score`, 1);
customStdout.muted = false; //allow output so we can draw
//testing screen size
let error = BEAST.checkSize();
if( error === '' ) {
let spaceTop = Math.floor( getSpaceTop() );
let spaceLeft = getSpaceLeft();
Readline.cursorTo( BEAST.RL, spaceLeft, (spaceTop + 4 + ( BEAST.MINHEIGHT - 6 )) ); //go to bottom of board
//calculate the space between lives and beast count
let spaceMiddle = ( BEAST.MINWIDTH - 2 ) - ( 3 * BEAST.LIVES ) - 3 - ( Object.keys( BEAST.BEASTS ).length.toString().length );
BEAST.RL.write(
`${Chalk.cyan(` ${BEAST.SYMBOLS.lives}`).repeat( BEAST.LIVES - BEAST.DEATHS )}` +
`${Chalk.gray(` ${BEAST.SYMBOLS.lives}`).repeat( BEAST.DEATHS )}` +
`${' '.repeat( spaceMiddle )} ${ Object.keys( BEAST.BEASTS ).length } x ${BEAST.SYMBOLS.beast}`
);
Readline.cursorTo( BEAST.RL, 0, (CliSize().rows - 1) ); //go to bottom of board and rest cursor there
}
customStdout.muted = true; //no more user output now!
},
Conclusion
The Node.js Readline allows you t o create an interactive command program to receive the users’ input and read the file content. Check the above methods to read input stream by Node.js readline.
Leave a comment