[英]How to make express.js using pm2 run only one instance on port (8080) and on second instance use another port (8081)
I searched all over, but could not find the answer for how to config pm2 with Express.js here is what i have so far based on other's answers and pm2 documentation. 我到处搜索,但找不到如何使用Express.js配置pm2的答案,这是到目前为止我根据其他人的答案和pm2文档得出的结论。
this is on the server main file(index.js): 这在服务器主文件(index.js)上:
const port = process.env.NODE_PORT; const port = process.env.NODE_PORT;
I get undefined unless i use || 除非使用||,否则我将无法定义 8080 .
8080。
const port = process.env.NODE_PORT || const port = process.env.NODE_PORT || 8080;
8080;
I need it working only on dev env for now.. 我需要它目前仅在dev env上工作。
but it seems it does not get what i config on ecosystem.config.js file. 但似乎没有得到我在ecosystem.config.js文件上配置的内容。 and on my ecosystem.config.js:
在我的ecosystem.config.js上:
module.exports = {
apps: [{
name: 'API',
script: 'index.js',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
args: 'one two',
instances: 1,
exec_mode: "fork_mode",
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_PORT = 8080 `pm2 start app.js -f`,
NODE_PORT = 8081 `pm2 start app.js -f`,
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'
}
},
{
name: 'API',
script: 'index.js',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
args: 'one two',
instances: 1,
exec_mode: "fork_mode",
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
PORT: 8081,
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'
}
}
],
deploy: {
production: {
user: 'node',
host: '212.83.163.1',
ref: 'origin/master',
repo: 'git@github.com:repo.git',
path: '/var/www/production',
'post-deploy': 'npm install && pm2 reload ecosystem.config.js --env
production'
}
}
};
I'm using environment variable process.env.NODE_APP_INSTANCE
to do this. 我正在使用环境变量
process.env.NODE_APP_INSTANCE
来执行此操作。 ( https://pm2.io/doc/en/runtime/guide/load-balancing/#cluster-environment-variable ) ( https://pm2.io/doc/zh/runtime/guide/load-balancing/#cluster-environment-variable )
I'm setting my PORT
before starting my server, and then I'm setting server port based on PORT
environment variable and NODE_APP_INSTANCE
, something like this: 我在启动服务器之前先设置
PORT
,然后根据PORT
环境变量和NODE_APP_INSTANCE
设置服务器端口,如下所示:
const nodeInstance = parseInt(process.env.NODE_APP_INSTANCE || 0, 10);
const port = process.env.PORT + nodeInstance;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.