var requestDomainWrapper =require('myhr-request-domain-wrapper-middleware');
# before app.use(app.routes)
app.use(requestDomainWrapper);
How it works
Exports a function matching the signature of express middleware. Binds incoming request & response from express to a new domain. Assigns the domain a unique id.
If the domain emits an error event, requestDomainWrapper will call next(err) with the error event from the domain. This allows existing express specific error handling middleware to
function as if the error was hanlded by your application code. Allow me to demonstrate with an example:
///old-school
app.get('/error',function(req,res,next){
db.query('SELECT happiness()',function(err,rows){
if(err)returnnext(err);
fs.readFile('alskdjflasd',function(err,contents){
if(err)returnnext(err);
process.nextTick(function(){
thrownewError("congratulations, your node process crashed and the user request disconnected in a jarring way");