The Node Foundation did deprecate the domain API, and for good reasons. The domains were mainly here to ensure that in case of a thrown error, in the context of an http server, you would not leave sockets hanging, and plainly stop every single request still being processed asynchronously. This is due to the fact that error handling in node is far from perfect and hard to set up. It was a hack intended to be dropped as soon as possible.
Promises allow for contained throws that will not contaminate other
asynchronous operations. The catch()
method will allow you to deal
with your exceptions gracefully.
http.createServer((req, res) => {
const d = domain.create();
d.on('error', yourRecoveryFunction)
d.run(yourHandler(req, res));
});
http.createServer((req, res) => {
Promise.resolve(yourHandler(req, res)).catch(yourRecoveryFunction);
});