node-continuation-local-storage
node-continuation-local-storage copied to clipboard
Can't configure with express 4
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)
- 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?)
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
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:
Hello! Im having a similar issue!
http://stackoverflow.com/questions/25982818/nodejs-express-adding-transaction-ids
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.
I can confirm that @arthurschreiber's suggestion is correct because this is working fine when using https://github.com/othiym23/cls-middleware
I would like to confirm again that @arthurschreiber solution solve the problem! Thanks.