我有一个非常基本的带有快递服务器的nodejs应用程序,它可以做到这一点。
const express = require('express');
const PORT = 4000;
const app = express();
app.listen(PORT, function(err) {
if(!err) {
console.log("Connected on ${PORT}");
}
});
我的pm2
配置如下:
module.exports = {
apps : [{
name : 'node-webpack-babel-starter',
script : './dist/backend.js',
env: {
NODE_ENV: 'development'
},
env_production : {
NODE_ENV: 'production'
},
instances: 'max',
output: './logs/out.log',
error: './logs/error.log',
log: './logs/combined.outerr.log',
}],
deploy : {
production : {
user : 'node',
host : '<>',
ref : 'origin/master',
repo : 'git@github.com:repo.git',
path : './dist/backend.js',
'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production'
}
}
};
根据pm2-runtime,我们可以使用以下命令在生产环境中启动服务器。
pm2-runtime start ecosystem.config.js --env production
我尝试在本地机器上运行,只有一个实例启动,所有其他实例失败。 下面是日志:
[2018-08-22T07:50:13.285Z] PM2 log: Launching in no daemon mode
[2018-08-22T07:50:13.349Z] PM2 log: Starting execution sequence in -fork mode- for app name:node-webpack-babel-starter id:0
[2018-08-22T07:50:13.357Z] PM2 log: App name:node-webpack-babel-starter id:0 online
[2018-08-22T07:50:13.361Z] PM2 log: Starting execution sequence in -fork mode- for app name:node-webpack-babel-starter id:1
[2018-08-22T07:50:13.367Z] PM2 log: App name:node-webpack-babel-starter id:1 online
[2018-08-22T07:50:13.369Z] PM2 log: Starting execution sequence in -fork mode- for app name:node-webpack-babel-starter id:2
[2018-08-22T07:50:13.380Z] PM2 log: App name:node-webpack-babel-starter id:2 online
[2018-08-22T07:50:13.391Z] PM2 log: Starting execution sequence in -fork mode- for app name:node-webpack-babel-starter id:3
[2018-08-22T07:50:13.411Z] PM2 log: App name:node-webpack-babel-starter id:3 online
Example app listening on port 4000!
Example app listening on port 4000!
[2018-08-22T07:50:13.730Z] PM2 log: App [node-webpack-babel-starter] with id [2] and pid [22967], exited with code [1] via signal [SIGINT]
[2018-08-22T07:50:13.733Z] PM2 error: Cancelling versioning data parsing
[2018-08-22T07:50:13.734Z] PM2 log: Starting execution sequence in -fork mode- for app name:node-webpack-babel-starter id:2
Error: listen EADDRINUSE :::4000
at Server.setupListenHandle [as _listen2] (net.js:1335:14)
at listenInCluster (net.js:1383:12)
at Server.listen (net.js:1470:7)
at Function.listen (/home/box/mien/webpack-node/node_modules/express/lib/application.js:618:24)
at Object.listen (/home/box/mien/webpack-node/dist/webpack:/src/main.js:9:5)
at __webpack_require__ (/home/box/mien/webpack-node/dist/webpack:/webpack/bootstrap:22:1)
at module.exports (/home/box/mien/webpack-node/dist/webpack:/webpack/bootstrap:74:1)
at Object.<anonymous> (/home/box/mien/webpack-node/dist/backend.js:78:10)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
Example app listening on port 4000!
[2018-08-22T07:50:13.754Z] PM2 log: App name:node-webpack-babel-starter id:2 online
Error: listen EADDRINUSE :::4000
有趣的是,当我运行 pm2 start
时,它工作正常。
pm2 start
上的更新事件只有一个实例启动,所有其他实例都已断开连接到4000
端口的连接。
答案 0 :(得分:1)