Problem: We start pm2 in cluster mode, and pm2 starts as many processes as there are cpu cores, pm2 also tries to start as many node servers as there are cpu cores but the problem here is that it fails to start as many servers because they all try and start on the same port that is 3000, which already gets occupied by the first node server
We using nginx and proxy it to 3000 port.
we are using pm2 in cluster mode with the following configuration:
{
"apps" : [{
"script" : "npm",
"instances" : "max",
"cwd":"/home/nginx/my-pwa" ,
"args" : "run start:server:prod",
"exec_mode" : "cluster",
"wait_ready": true,
"kill_timeout" : 4000,
"watch" : true
}]
}
run start:server:prod is our script to start the server
Our express server:
var app = require('../src/app');
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
const http = require('http');
server = http.createServer(app);
server.listen(port));
server.on('error', onError);
server.on('listening', onListening);
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
process.on('message', function(msg) {
if (msg == 'shutdown') {
server.close();
process.exit(0);
}
});
// Listening logic
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
console.log("Server started on ", bind);
process.send('ready');
}
Please help, it's mission critical!