我有一个用Node.js编写的web服务器,我想用一个特定的文件夹启动。我不知道如何在JavaScript中访问参数。我运行的节点如下:
$ node server.js folder
这里server.js是我的服务器代码。Node.js帮助说这是可能的:
$ node -h
Usage: node [options] script.js [arguments]
如何在JavaScript中访问这些参数?不知怎么的,我在网上找不到这些信息。
我有一个用Node.js编写的web服务器,我想用一个特定的文件夹启动。我不知道如何在JavaScript中访问参数。我运行的节点如下:
$ node server.js folder
这里server.js是我的服务器代码。Node.js帮助说这是可能的:
$ node -h
Usage: node [options] script.js [arguments]
如何在JavaScript中访问这些参数?不知怎么的,我在网上找不到这些信息。
当前回答
您可以使用system.args获取命令行参数。我使用下面的解决方案将参数解析为一个对象,这样我就可以按名称获取所需的参数。
var system = require('system');
var args = {};
system.args.map(function(x){return x.split("=")})
.map(function(y){args[y[0]]=y[1]});
现在你不需要知道参数的索引。像args.whatever一样使用它
注意:应该使用file.js x=1y=2这样的命名参数这一解决方案。
其他回答
标准方法(无库)
参数存储在process.argv中
以下是关于处理命令行参数的节点文档:
process.argv是一个包含命令行参数的数组。第一个元素是“node”,第二个元素是JavaScript文件的名称。接下来的元素将是任何其他命令行参数。
// print process.argv
process.argv.forEach(function (val, index, array) {
console.log(index + ': ' + val);
});
这将产生:
$ node process-2.js one two=three four
0: node
1: /Users/mjr/work/node/process-2.js
2: one
3: two=three
4: four
Simple+ES6+无依赖项+支持布尔标志
const process = require( 'process' );
const argv = key => {
// Return true if the key exists and a value is defined
if ( process.argv.includes( `--${ key }` ) ) return true;
const value = process.argv.find( element => element.startsWith( `--${ key }=` ) );
// Return null if the key does not exist and a value is not defined
if ( !value ) return null;
return value.replace( `--${ key }=` , '' );
}
输出:
如果使用节点app.js调用,则argv('fo')将返回null如果使用节点app.js--foo调用,则argv('fo')将返回true如果使用节点app.js调用--foo=,则argv('fo')将返回“”如果使用节点app.js--foo=bar调用,则argv('fo')将返回'bar'
传递、解析参数是一个简单的过程。Node为您提供process.argv属性,它是字符串数组,是调用Node时使用的参数。数组的第一个条目是Node可执行文件,第二个条目是脚本的名称。
如果您使用以下参数运行脚本
$ node args.js arg1 arg2
文件:args.js
console.log(process.argv)
您将获得类似数组的
['node','args.js','arg1','arg2']
使用最小npm包。这是最简单的方法,不需要担心任何事情。
const arguments = require("minimist")(process.argv.slice(2));
// get the extra argument of command line .
eg node app.js --process="sendEmailWithReminder"
我们也可以在windows任务调度程序中使用它。
命令行参数值得一看!
您可以使用主要符号标准设置选项(了解更多信息)。这些命令都是等效的,设置相同的值:
$ example --verbose --timeout=1000 --src one.js --src two.js
$ example --verbose --timeout 1000 --src one.js two.js
$ example -vt 1000 --src one.js two.js
$ example -vt 1000 one.js two.js
要访问这些值,首先创建一个选项定义列表,描述应用程序接受的选项。type属性是setter函数(提供的值通过该函数传递),使您能够完全控制接收的值。
const optionDefinitions = [
{ name: 'verbose', alias: 'v', type: Boolean },
{ name: 'src', type: String, multiple: true, defaultOption: true },
{ name: 'timeout', alias: 't', type: Number }
]
接下来,使用commandLineArgs()解析选项:
const commandLineArgs = require('command-line-args')
const options = commandLineArgs(optionDefinitions)
选项现在看起来如下:
{
src: [
'one.js',
'two.js'
],
verbose: true,
timeout: 1000
}
高级用法
除了上述典型用法外,还可以配置命令行参数以接受更高级的语法形式。
基于命令的语法(git样式),格式为:
$ executable <command> [options]
例如
$ git commit --squash -m "This is my commit message"
命令和子命令语法(docker样式),格式如下:
$ executable <command> [options] <sub-command> [options]
例如
$ docker run --detached --image centos bash -c yum install -y httpd
生成使用指南
可以使用命令行用法生成使用指南(通常在设置--help时打印)。请参阅下面的示例并阅读文档以了解如何创建它们。
典型的使用指南示例。
聚合物cli使用指南是一个很好的现实例子。
进一步阅读
还有很多需要学习的内容,请参见wiki获取示例和文档。