NodeJ在控制台中处理用户输入 [英] NodeJs handle user input in console

查看:118
本文介绍了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();
});

文档可用此处

这篇关于NodeJ在控制台中处理用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆