Skip to content

Instantly share code, notes, and snippets.

@adrianlee
Forked from netroy/000-server.js
Created January 9, 2014 11:57
Show Gist options
  • Save adrianlee/8333010 to your computer and use it in GitHub Desktop.
Save adrianlee/8333010 to your computer and use it in GitHub Desktop.
var cluster = require('cluster'),
app = require('./app');
var workers = {},
count = require('os').cpus().length;
function spawn(){
var worker = cluster.fork();
workers[worker.pid] = worker;
return worker;
}
if (cluster.isMaster) {
for (var i = 0; i < count; i++) {
spawn();
}
cluster.on('death', function(worker) {
console.log('worker ' + worker.pid + ' died. spawning a new process...');
delete workers[worker.pid];
spawn();
});
} else {
app.listen(process.env.app_port || 5000);
}
var express = require('express'),
app = module.exports = express.createServer();
app.configure(function(){
app.use(app.router);
});
app.get('/', function(req, resp){
resp.writeHead(200, "Content-type: text/json");
resp.write("Hello Node");
resp.end();
});
if (!module.parent) {
app.listen(process.env.app_port || 5000);
}
var connect = require('connect'),
app = module.exports = connect.createServer();
app.use(connect.router(function(router){
router.get("/", function(req, resp){
resp.writeHead(200, "Content-type: text/json");
resp.write("Hello Node");
resp.end();
});
}));
if (!module.parent) {
app.listen(process.env.app_port || 5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment