【问题标题】:How to read user input directly in Node.js [duplicate]如何在 Node.js 中直接读取用户输入 [重复]
【发布时间】:2022-02-12 18:32:05
【问题描述】:

从下面的代码中,我可以读取并 console.log 行

const readline = require('readline');
const { stdin: input, stdout: output } = require('process');

const rl = readline.createInterface({ input, output });

rl.question('What is your name? ', (answer) => {
  console.log(`Hi ${answer}!`);

  rl.close();
});

但我正在尝试像这样读取用户输入 输入:node app.js John 输出:Hi John!

如何在节点中实现这一点?

【问题讨论】:

标签: javascript node.js readline


【解决方案1】:

为此,您可以使用以下方法:

process.argv

process.argv 是一个数组,其中包含调用程序时使用的所有参数。 您这样调用程序的图像:node index.js "John",process.argv 看起来像这样:["node", "path/to/index.js", "John"]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    相关资源
    最近更新 更多