var createDomain = require('domain').create;
var app = express();
var times = 0;
function domainMiddleware () {
return function (req, res, next) {
var domain = createDomain();
domain.id = Date.now() + (times++);
domain.add(req);
domain.add(res);
domain.run(function() {
next();
});
domain.on('error', function(e) {
next(e);
});
}
}
app.use(domainMiddleware());