Skip to content

Instantly share code, notes, and snippets.

@gjohnson
Created June 6, 2013 19:15
Show Gist options
  • Select an option

  • Save gjohnson/5724104 to your computer and use it in GitHub Desktop.

Select an option

Save gjohnson/5724104 to your computer and use it in GitHub Desktop.
/**
* Dependencies.
*/
var Domain = require('domain');
var http = require('http');
var app = require('..');
/**
* Export `server`.
*/
var server = http.createServer(onRequest);
/**
* Listen.
*/
server.listen(process.env.PORT || 3000);
/**
* Request handler.
*
* Wraps `req` and `res` with a domain and forwards the request along to `app`.
*
* Note: If any strange errors happen or there is a sign of memory leaks, I
* would start here first as domains are pretty new.
*/
function onRequest(req, res){
var domain = Domain.create();
domain.on('error', function(error){
console.error(error.stack);
try {
var reaper = setTimeout(function(){
process.exit(1);
}, 10000);
reaper.unref();
server.close();
res.statusCode = 500;
res.end(error.message);
} catch (error) {
console.error(error.stack);
}
});
domain.add(req);
domain.add(res);
domain.run(function(){
app(req, res);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment