I
I
ILE2015-04-13 23:55:16
JavaScript
ILE, 2015-04-13 23:55:16

Error with catching asynchronous error module domain ( node js )?

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Shatokhin, 2015-04-14
@iShatokhin

Ideally, each request should have its own domain.

var createDomain = require('domain').create;
var app = express();
var times = 0;

function domainMiddleware () {
  return function (req, res, next) {
    var domain = createDomain();
    domain.id = Date.now() + (times++);
    domain.add(req);
    domain.add(res);
    domain.run(function() {
      next();
    });

    domain.on('error', function(e) {
      next(e);
    });
  }
}

app.use(domainMiddleware());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question