Use Node.js to read content from the terminal
This is the 17th day of my participation in Gwen Challenge
preface
At ordinary times when writing exercise algorithm questions, encounter online written test, most online exercise sites need to write their own input and output, such as the common niuke, Sai Code network. Some people who are not familiar with node.js input and output will feel powerless
Usually such sites provide a readline or read_line method for data entry
Node actually provides a module called readline: reads line by line to do this
The author has used only Leetcode, which only needs to realize functions when writing with JS, and does not need to consider how to input content from the keyboard
This article will share with you the use of Node-readline module through the terminal input content
Simple to use
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
// No newline output
process.stdout.write('Enter two numbers:')
// Listen for enter events
rl.on('line'.(str) = > {
// STR is the input
const [a, b] = str.split(' ')
console.log('and is:', (+a) + (+b));
// Closing the line-by-line read stream triggers the closing event
rl.close()
})
// Listen for close events
rl.on('close'.() = > {
console.log('Triggered a close event');
})
Copy the code
The code logic is as follows:
- Enter two numbers
- Calculate the sum of the two figures
Assuming the file above is called index.js, let’s just type and run below
node index.js
Copy the code
The running results are as follows:
It’s not as comfortable to use as other languages, like:
- C++:
cin>>param
- C:
scanf("%s",¶m)
- C#:
param = Console.ReadLine()
- . , etc.
How do you make it as easy for Node.js to input data from a terminal as the language above?
We wrap it with our fingers
encapsulation
const rdl = require('readline')
/** * reads a line */
function readline() {
const rl = rdl.createInterface({
input: process.stdin,
output: process.stdout
})
return new Promise(resolve= > {
rl.on('line'.(str) = > {
resolve(str)
rl.close()
})
})
}
module.exports = readline
Copy the code
We are writing a new test.js, and the one above is called read.js in the same directory
const readline = require('./readline')
async function main(){
process.stdout.write('First word:')
const a = await readline()
process.stdout.write('Second word:')
const b = await readline()
console.log(a , b);
}
main()
Copy the code
node test.js
Copy the code
The results
Now it’s a lot easier to use, it’s a little bit of another language
Other tool method encapsulation
util.js
A printing method that does not wrap
function print(str){
process.stdout.write(str)
}
Copy the code
Use:
print('hello world\n')
print('666\n')
Copy the code
readNumber
Read a number. Since readLine reads everything as a string, it needs to convert itself to read a number
const readline = require('./readline')
async function readNumber(){
return+ (await readline())
}
Copy the code
Testing:
async function main(){
const a = await readline()
const b = await readNumber()
console.log('a'.typeof a);
console.log('b'.typeof b);
}
main()
Copy the code
The results
Methods summarize
We can create an index.js file and export the readline, readNumber, and print methods
const rdl = require('readline')
function print(str){
process.stdout.write(str)
}
/** * reads a line */
function readline() {
const rl = rdl.createInterface({
input: process.stdin,
output: process.stdout
})
return new Promise(resolve= > {
rl.on('line'.(str) = > {
resolve(str)
rl.close()
})
})
}
async function readNumber(){
return+ (await readline())
}
module.exports = {
print,
readline,
readNumber
}
Copy the code
Practice: Guess the numbers
TODO: Waiting for completion
Data summary
- Readline module: reads line by line
- Article complete sample source code