I believe the following code might be useful to handle exceptions that occur to certain requests:
var domain = require('domain');
app.use(function(req,res,next){
var d = domain.create();
d.on('error',function(err){
res.json({error: err.stack});
});
d.run(function(){
next();
});
});
however, I read that this type of code can leak memory. I am not sure I understand why. Is there a way to avoid leaking memory? Perhaps I should manually remove all the event handling and listeners for the domain after the response stream is closed? How do I allow the domain object to be garbage collected?