我试图将pm2进程管理器与ExpressJS应用程序一起使用。第一个实例运行并正常工作,但其他实例连续与EADDRINUSE崩溃并一次又一次地运行。所以我想知道我做错了什么?
我用下一个命令运行pm2:
pm2 start process.json
pm2的process.json配置文件如下所示:
{
"apps": [
{
"name": "clinical-trials",
"script": "./bin/www",
"instances": 0,
"env": {
"PORT" : 4000,
"NODE_ENV": "development"
},
"env_production": {
"PORT": 4444,
"NODE_ENV": "production"
},
"env_local": {
"PORT" : 4000,
"NODE_ENV": "local"
}
}
]
}
Express ./bin/www
包含下一个代码:
var port = normalizePort(process.env.PORT || app.locals.config.port);
app.set('port', port);
var server = http.createServer(app);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
在PM2 repo的示例文件夹中,他们有监听8000端口的子进程: https://github.com/Unitech/pm2/blob/master/examples/child.js 和运行4个实例的配置文件: https://github.com/Unitech/pm2/blob/master/examples/apps/all-pm2.json
答案 0 :(得分:2)
问题在于pm2试图以fork模式而不是集群运行实例。要解决此问题,我必须将"exec_mode": cluster
添加到process.json
文件中。 :
{
"apps": [
{
"name": "clinical-trials",
"script": "./bin/www",
"instances": 0,
"exec_mode": "cluster",
"env": {
"PORT" : 4000,
"NODE_ENV": "development"
},
"env_production": {
"PORT": 4444,
"NODE_ENV": "production"
},
"env_local": {
"PORT" : 4000,
"NODE_ENV": "local"
}
}
]
}