node.js 如何为pm2指定端口号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31502351/
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
How to specify a port number for pm2
提问by Luke W
I'm trying to use pm2 to manage a node.js cluster
我正在尝试使用 pm2 来管理 node.js 集群
pm2 start . -i 3
I'm currently running the app on heroku and using a Procfile with the above command, but I cannot figure out how to configure pm2 to use the existing PORT env var. Something like pm2 start . -p $PORT
我目前正在 heroku 上运行该应用程序并使用带有上述命令的 Procfile,但我无法弄清楚如何配置 pm2 以使用现有的 PORT env var。就像是pm2 start . -p $PORT
What am I missing?
我错过了什么?
回答by stdob--
You can use environment variable. For example:
您可以使用环境变量。例如:
1) NODE_PORT=3002 pm2 start -I 0 app.js
1) NODE_PORT=3002 pm2 start -I 0 app.js
2) Read value in app:
2)在应用程序中读取值:
console.log(process.env.NODE_PORT);
console.log(process.env.NODE_PORT);
Or, if you are build express app:
或者,如果您正在构建快速应用程序:
1) PORT=3002 pm2 start -I 0 ./bin/www
1) PORT=3002 pm2 start -I 0 ./bin/www
2) Express load PORT automatically at start application.
2) 在启动应用程序时自动快速加载 PORT。
回答by Semir Hodzic
You need to use -- to tell pm2 to stop parsing his options and give the rest to the program, then when you spawn direct binary, you need to tell pm2 that you don't want to use nodejs, so :
您需要使用 -- 告诉 pm2 停止解析他的选项并将其余部分交给程序,然后当您生成直接二进制文件时,您需要告诉 pm2 您不想使用 nodejs,因此:
pm2 start rethinkdb --interpreter none -- --port 8082
pm2 start rethinkdb --interpreter none -- --port 8082
You see you need -- --port 8082
你看你需要 -- --port 8082

