【问题标题】:How to convert API (route) to CLI on expressjs如何在 expressjs 上将 API(路由)转换为 CLI
【发布时间】:2017-12-21 11:47:45
【问题描述】:

例如: 我有这样的路线:

http://localhost:3000/source1/test

现在我想通过以下方式运行它:

node index.js --first=source1 --second=test

我该怎么做?

【问题讨论】:

    标签: node.js api express routes command-line-interface


    【解决方案1】:

    我们可以通过使用模块来做到:

    command-line-args

    const commandLineArgs = require('command-line-args');
    const optionDefinitions = [
      {name: 'route', alias: 'r', type: String}
    ];
    const option = commandLineArgs(optionDefinitions);
    

    然后我们就可以使用switch case来做我们想要的功能了。但是,我们必须将所有 API 转换为带有参数的普通函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-25
      • 2015-04-02
      • 2023-03-25
      • 2013-11-05
      相关资源
      最近更新 更多