othiym23 / cls-middleware

Connect & Restify middleware to bind routes to continuation-local storage
https://npmjs.org/package/cls-middleware
20 stars 10 forks source link

req and res should be bound inside the new context #8

Open coltrane opened 9 years ago

coltrane commented 9 years ago

The existing code binds req and res before starting a new context:

module.exports = function clsify(ns) {
  if (!ns) throw new Error('CLS namespace required');

  return function (req, res, next) {
    ns.bindEmitter(req);
    ns.bindEmitter(res);

    ns.run(function () {
      next();
    });
  };
};

But it seems that these should be bound within the new context. Something like this...

module.exports = function clsify(ns) {
  if (!ns) throw new Error('CLS namespace required');

  return function (req, res, next) {
    ns.run(function () {
      ns.bindEmitter(req);
      ns.bindEmitter(res);
      next();
    });
  };
};
marcelboettcher commented 9 years ago

+1