NodeJ在控制台中处理用户输入
问题描述:
我是NodeJ的新手,我习惯C#,因为我们可以使用 Console.ReadLine();
I'm new to NodeJs and i'm used to C# were we can use
Console.ReadLine();
我查看了 readline和节点提示包,但输入时两次输出所有用户输入,或者使用 terminal:false选项,不允许我们使用退格键。
I looked into 'readline' and the node prompt package, but it either outputs all the user input twice while entering or, with the 'terminal: false' option, does not allow us to use the backspace.
答
有一个阅读行,您可以像这样使用它:
There is a readline, you can use it like this:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('How are you today? ', (answer) => {
// TODO: Log the answer in a database
console.log(`Thank you for your valuable feedback: ${answer}`);
rl.close();
});
文档可用此处。