diff --git a/server.js b/server.js index 12e8bbe2..2968f185 100644 --- a/server.js +++ b/server.js @@ -1,7 +1,22 @@ +var cluster = require('cluster'); var app = require('./app'); -// Listen on port 3000 -var port = process.env.PORT || 3000; -app.listen(port, null, function() { - console.log('Server started: http://localhost:' + port); -}); +var count = require('os').cpus().length; + +if(!process.env.NO_CLUSTER && cluster.isMaster) { + for(var i = 0; i < count; i++) { + cluster.fork(); + } + cluster.on('exit', function(worker) { + console.log('Worker died. Spawning a new process...'); + cluster.fork(); + }); +} +else { + // Listen on port 3000 + var port = process.env.PORT || 3000; + app.listen(port, null, function() { + console.log('Server started: http://localhost:' + port); + }); +} +