Last active
August 17, 2019 18:10
-
-
Save ORESoftware/8c6900857e78189892429099a439eeac to your computer and use it in GitHub Desktop.
Using Node.js domains in production
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.use(function(req,res,next){ | |
const d = Domain.create(); // create a new domain for this request | |
res.once('finish', function () { | |
d.exit(); | |
d.removeAllListeners(); | |
}); | |
d.once('error', function (e) { | |
if (!res.headersSent) { | |
res.status(500).json({ | |
error: e && e.stack || util.inspect(e || 'no error trace available') | |
}); | |
} | |
}); | |
// we invoke the next middleware | |
d.run(next); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment