How can I create Express/Connect middleware which wrap each request in its own domain?
This set of slides on Speaker Deck gives a succinct overview:
Express middleware code from the slides:
var createDomain = require('domain').create;
app.use(function(req, res, next) {
var domain = createDomain();
domain.on('error', function(err) {
// alternative: next(err)
res.statusCode = 500;
res.end(err.message + '\n');
domain.dispose();
});
domain.enter();
next();
});