Skip to content

Instantly share code, notes, and snippets.

@alaahaytham
Last active August 20, 2016 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alaahaytham/be297fb0021a239327832f0e5cfac31e to your computer and use it in GitHub Desktop.
Save alaahaytham/be297fb0021a239327832f0e5cfac31e to your computer and use it in GitHub Desktop.
var express = require('express');
var cluster = require('cluster');
if (cluster.isMaster) {
var _cpus = require('os').cpus().length;
// create a worker for each CPU
for (var i = 0; i < _cpus; i += 1) {
cluster.fork();
}
// When a worker dies create another one
cluster.on('exit', function(worker) {
console.log('worker ' + worker.id + ' died');
cluster.fork();
});
} else {
// create a new Express application
var app = express();
app.get('/', function (req, res) {
res.send('Hello World');
});
// bind to a port
app.listen(3000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment