【发布时间】: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
例如: 我有这样的路线:
http://localhost:3000/source1/test
现在我想通过以下方式运行它:
node index.js --first=source1 --second=test
我该怎么做?
【问题讨论】:
标签: node.js api express routes command-line-interface
我们可以通过使用模块来做到:
const commandLineArgs = require('command-line-args');
const optionDefinitions = [
{name: 'route', alias: 'r', type: String}
];
const option = commandLineArgs(optionDefinitions);
然后我们就可以使用switch case来做我们想要的功能了。但是,我们必须将所有 API 转换为带有参数的普通函数。
【讨论】: