例如: 我有这样的路线:
http://localhost:3000/source1/test
现在我想通过以下方式运行它:
node index.js --first=source1 --second=test
我该怎么做?
最佳答案
我们可以通过使用模块来做到:
const commandLineArgs = require('command-line-args');
const optionDefinitions = [
{name: 'route', alias: 'r', type: String}
];
const option = commandLineArgs(optionDefinitions);
然后我们就可以使用switch case来实现我们想要的功能了。但是,我们必须将所有 API 转换为带有参数的普通函数。
关于node.js - 如何在expressjs上将API(路由)转换为CLI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45135776/