cls-middleware icon indicating copy to clipboard operation
cls-middleware copied to clipboard

req and res should be bound inside the new context

Open coltrane opened this issue 9 years ago • 1 comments

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();
    });
  };
};

coltrane avatar Aug 25 '15 20:08 coltrane

+1

marcelboettcher avatar Sep 30 '15 14:09 marcelboettcher