Node.js process.domain is undefined sometimes

node.js

Solution

Just found out, that explisity binding req and res to domain at creation moment, solves undefined domains down in execution stack.

app.use(function (req, res, next) {
  // create per request domain instance
  domain.create().run(function() {
    // explicit binding
    process.domain.add(req)
    process.domain.add(res)
    // save request to domain, to make it accessible everywhere
    process.domain.req = req;
    process.domain.res = res;
    next()
  });
});

Don't know why this happens, so I leave question open for other possible advices.

Problem

In Node.js project build with LoopbackJS I need to store data during request. So I used domain feature: ``` // pre-processing middleware app.use(function (req, res, next) { // create per request domain instance var domain = require('domain').create(); // save request and response to domain, to make it accessible everywhere domain.req = req; domain.res = res; domain.run(next); }); ``` Later in required module: ``` Model.beforeRemote('**', function(oContext, oModel, next) { // Save method name for later use process.domain.remoteContext = { /* Here is an error thrown */ methodName: oContext.method.name }; ... process.domain.res.send() // example of usage }) ``` BUT when I make request from Safari or IE, process.domain is sometimes undefined! Requesting from Chrome or Firefox works as expected. Any suggestions? Error response: ``` {"error":{"name":"TypeError","status":500,"message":"Cannot set property 'remoteContext' of undefined","stack":"TypeError: Cannot set property 'remoteContext' of undefined\n at module.exports (/Users/igormatyushkin/projects/Yash/server/hooks/admin-remote.js:12:34)\n at Function.Model.setup.ModelCtor.beforeRemote.args (/Users/igormatyushkin/projects/Yash/node_modules/loopback/lib/model.js:184:9)\n at execStack (/Users/igormatyushkin/projects/Yash/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:363:13)\n at RemoteObjects.execHooks (/Users/igormatyushkin/projects/Yash/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:372:10)\n at RemoteObjects.invokeMethodInContext (/Users/igormatyushkin/projects/Yash/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:512:8)\n at async.series.results (/Users/igormatyushkin/projects/Yash/node_modules/loopback/node_modules/strong-remoting/node_modules/async/lib/async.js:610:21)\n at _asyncMap (/Users/igormatyushkin/projects/Yash/node_modules/loopback/node_modules/strong-remoting/node_modules/async/lib/async.js:249:17)\n at async.eachSeries.iterate (/Users/igormatyushkin/projects/Yash/node_modules/loopback/node_modules/strong-remoting/node_modules/async/lib/async.js:149:13)\n at async.eachSeries (/Users/igormatyushkin/projects/Yash/node_modules/loopback/node_modules/strong-remoting/node_modules/async/lib/async.js:165:9)\n at _asyncMap (/Users/igormatyushkin/projects/Yash/node_modules/loopback/node_modules/strong-remoting/node_modules/async/lib/async.js:248:13)"}} ```

Original source