[英]How to make express.js using pm2 run only one instance on port (8080) and on second instance use another port (8081)
我到處搜索,但找不到如何使用Express.js配置pm2的答案,這是到目前為止我根據其他人的答案和pm2文檔得出的結論。
這在服務器主文件(index.js)上:
const port = process.env.NODE_PORT;
除非使用||,否則我將無法定義 8080。
const port = process.env.NODE_PORT || 8080;
我需要它目前僅在dev env上工作。
但似乎沒有得到我在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'
}
}
};
我正在使用環境變量process.env.NODE_APP_INSTANCE
來執行此操作。 ( https://pm2.io/doc/zh/runtime/guide/load-balancing/#cluster-environment-variable )
我在啟動服務器之前先設置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.