node-continuation-local-storage icon indicating copy to clipboard operation
node-continuation-local-storage copied to clipboard

Can't configure with express 4

Open santimacia opened this issue 10 years ago • 7 comments

I'm having troubles with configuration under express4. I tried at the beginning and in the end with same result. I followed the instructions of issue #15

This is the code I tried:

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

});
app.use(function(req, res) {
  return ns.set('traceId', 'something');


});
app.get('/traced', function(req, res) {
   console.log("traceId = " + (ns.get('traceId')));
   res.send('ok')
});

And the stack error:

TypeError: Object object has no method 'toString'
    at node_modules/express/lib/application.js:140:28
    at node_modules/express/lib/router/index.js:140:5
    at node_modules/express/lib/router/index.js:265:10
    at next (node_modules/express/lib/router/index.js:165:14)
    at next (node_modules/express/lib/router/index.js:182:38)
    at next (node_modules/express/lib/router/index.js:182:38)
    at next (node_modules/express/lib/router/index.js:182:38)
    at next (node_modules/express/lib/router/index.js:182:38)
    at next (node_modules/express/lib/router/index.js:182:38)
    at next (node_modules/express/lib/router/index.js:182:38)

santimacia avatar May 22 '14 14:05 santimacia

  • What version of Express?
  • What version of Node?
  • What's the setup code for ns?
  • How are you requiring continuation-local-storage? (i.e. where in the file / your app are you including it?)

othiym23 avatar May 22 '14 15:05 othiym23

node v0.10.22 [email protected]

I've just tried to make a simple example to start, but I have no idea how to configure. I will use for passing the values in mongoose requests later.

https://gist.github.com/encaputxat/72ab0100f9908270d490

santimacia avatar May 22 '14 19:05 santimacia

I suspect the issue is return ns.run(next);. Change it to return ns.run(function() { next(); }); and it should work. ns.run passes the context object to the callback it's given, and next treats the first argument as an error object. :boom:

arthurschreiber avatar Aug 22 '14 13:08 arthurschreiber

Hello! Im having a similar issue!

http://stackoverflow.com/questions/25982818/nodejs-express-adding-transaction-ids

vasiliyb avatar Sep 23 '14 20:09 vasiliyb

Pretty sure that @arthurschreiber has it, although there may be some fancy continuation stuff that Express 4 is doing differently from Express 3 / Connect 2 and earlier. I'm sorry for neglecting this issue; I've been slammed with npm stuff and haven't had time to put the time I want into CLS. If somebody could put together a failing test case for me, that would be very helpful.

othiym23 avatar Sep 25 '14 03:09 othiym23

I can confirm that @arthurschreiber's suggestion is correct because this is working fine when using https://github.com/othiym23/cls-middleware

antonmos avatar Mar 19 '15 20:03 antonmos

I would like to confirm again that @arthurschreiber solution solve the problem! Thanks.

ckitt avatar Oct 16 '17 02:10 ckitt