node.js 如何使用 pm2 将参数传递给应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28980307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:31:35  来源:igfitidea点击:

How to pass arguments to app using pm2?

node.jsexpressforeverpm2

提问by user3373581

I am using pm2 to start my app but i am not able to pass argument to it. the command I am using is pm2 start app.js -- dev. Though this works with forever.

我正在使用 pm2 来启动我的应用程序,但我无法向它传递参数。我使用的命令是 pm2 start app.js -- dev。虽然这适用于永远。

采纳答案by tsturzl

You can do as stated in this ticket: https://github.com/Unitech/pm2/issues/13

您可以按照这张票中的说明进行操作:https: //github.com/Unitech/pm2/issues/13

Though if you're passing the environment you may want to consider leveraging environment variables. With this you create a variable which can be accessed by any process in that environment with process.env.*.

但是,如果您要传递环境,您可能需要考虑利用环境变量。有了这个,您可以创建一个变量,该环境中的任何进程都可以使用process.env.*.

So you have a configuration file config.json:

所以你有一个配置文件config.json

{
   "dev": {
        "db": {
            "hosts":["localhost"],
            "database": "api"
        },
        "redis": {
            "hosts": ["localhost"]
        }
   },
   "staging": {
        "db": {
            "hosts":["1.1.1.1"],
            "database": "api"
        },
        "redis": {
            "hosts": ["2.2.2.2"]
        }
   },
   "production": {
        "db": {
            "hosts":["1.1.1.1", "1.1.1.2", "1.1.1.3"],
            "database": "api"
        },
        "redis": {
            "hosts": ["2.2.2.2", "2.2.2.3"]
        }
   }
}

Then you import your config:

然后你导入你的配置:

var config=require('./config.json')[process.env.NODE_ENV || 'dev'];

db.connect(config.db.hosts, config.db.database);

Then you'd set the variable in your environment via shell:

然后你可以通过 shell 在你的环境中设置变量:

export NODE_ENV=staging
pm2 start app.js

The environment variable will last as long as your session. So you'll have to set it in the ~/.bashrcfile for that user for the variable to persist. This will set the variable every session.

环境变量将持续与您的会话一样长。因此,您必须在该~/.bashrc用户的文件中设置它才能使变量保持不变。这将在每个会话中设置变量。

PM2 has a deploy systemwhich allows you to set an environment variable each time before your app is daemonized. This is how daemons in POSIX systems typically take parameters, because those parameters aren't lost with the process. Given with your circumstance it might not matter so much, but its a good practice.

PM2 有一个部署系统,它允许你每次在你的应用程序被守护之前设置一个环境变量。这就是 POSIX 系统中的守护进程通常采用参数的方式,因为这些参数不会随进程丢失。鉴于您的情况,这可能无关紧要,但这是一个很好的做法。

Moreover you should consider stop/starting locally, and restarting(if in cluster mode) whenever possible to prevent downtime when in production.

此外,您应该考虑在本地停止/启动,并尽可能重新启动(如果在集群模式下)以防止在生产中停机。

回答by Nishchit Dhanani

If you want to pass node arguments from CLI then

如果你想从 CLI 传递节点参数,那么

pm2 start myServer.js --node-args="--production --port=1337"

.

.

Edited

已编辑

you can add any arguments after --

您可以在之后添加任何参数 --

pm2 start app.js -- --prod

Sails docs for deploymemt.

用于deploymemt 的Sails 文档。

回答by Travis

It is possible to define arguments with the process.

可以定义过程的参数。

You can define a new process in ecosystem.config.jswith an argskey, like so:

您可以ecosystem.config.js使用args密钥定义一个新进程,如下所示:

{
  name            : 'my-service',
  script          : './src/service.js',
  args            : 'firstArg secondArg',
},
{
  name            : 'my-service-alternate',
  script          : './src/service.js',
  args            : 'altFirstArg altSecondArg',
}

Here, the two processes use the same file (service.js), but pass different arguments to it.

在这里,两个进程使用同一个文件 ( service.js),但向它传递不同的参数。

Note that these arguments are handled within service.js. In my case I just used process.argv[2]to get the first argument, and so on.

请注意,这些参数在service.js. 在我的情况下,我只是用来process.argv[2]获取第一个参数,依此类推。

回答by Alex

You can send arguments to your script by passing them after --. For example: pm2 start app.js -i max -- -a 23 // Pass arguments after -- to app.js

您可以通过在--. 例如:pm2 start app.js -i max -- -a 23 // Pass arguments after -- to app.js

回答by Xin

Well there are 2 ways you can do to pass the parameters from pm2 to nodejs in CLI:

好吧,您可以通过两种方法在 CLI 中将参数从 pm2 传递到 nodejs:

  • pm2 start app.js -- dev --port=1234 (note there is an extra space between -- and dev)
  • pm2 start app.js --node-args="dev --port=1234"
  • pm2 start app.js -- dev --port=1234(注意 -- 和 dev 之间有一个额外的空格)
  • pm2 启动 app.js --node-args="dev --port=1234"

Both ways, you will find these values exist in process.argv(['dev','--port=1234'])

两种方式,你都会发现这些值存在于process.argv(['dev','--port=1234'])

回答by kenny

From the pm2docs

来自pm2文档

//Inject what is declared in env_production
$ pm2 start app.js --env production 

//Inject what is declared in env_staging
$ pm2 restart app.js --env staging

回答by onsmak

You can pass args for node just like that:

您可以像这样为节点传递参数:

NODE_TLS_REJECT_UNAUTHORIZED=0 NODE_ENV=dev pm2 start server.js --name web-server

回答by Daian Gan

You need to start pm2 with something like: pm2 start app.js --name "app_name" -- arg1 arg2

您需要使用以下内容启动 pm2: pm2 start app.js --name "app_name" -- arg1 arg2

Then in your code, you can get your args with: console.log(process.argv);

然后在您的代码中,您可以使用以下命令获取参数: console.log(process.argv);

process.argv is a list like this: [ '/usr/local/bin/node', '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', 'arg1', 'arg2' ]

process.argv 是这样的列表: [ '/usr/local/bin/node', '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', 'arg1', 'arg2' ]