-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Context gets lost in Express after using session middleware #29
Comments
The first 2 examples are probably a little oversimplified, but what they're intended to convey is the idea that somewhere else something is calling
I would have to dig into the source of app.use(
ns.bind(
session({
resave : true,
saveUninitialized: true,
secret: 'someSecret',
store: sessionStore
})
)
); That may not work (see above for the cleverness going on inside @wraithan or @hayes, have either of you run into this with Express 4? Might you have any pointers here? |
Thanks for the prompt reply, Forrest. Much appreciated. Unfortunately, the suggested In regards to:
I'll see if I can come up with a suitable patch. Cheers. |
Maybe not a real help but at least a comment: That's working like a charm with the usual (IMHO this should be added to the app.use(function initializeContinuationLocalStorage(req, res, next) {
ns.bindEmitter(req);
ns.bindEmitter(res);
ns.run(function() {
next();
});
}); Therefore express itself seems to be fine and it's |
@nasooz not sure if you typo-ed there, but wanted to highlight the differences in what you indicated didn't work and what was suggested. @othiym23 indicated you use the ns.bind() wrapper around the already existing session function. ns.bind(session) In your reply where you said it didnt work you wrote ns.session() , which does not use the bind method defined in the source https://github.com/othiym23/node-continuation-local-storage/blob/master/context.js I believe ns.session() would cause an uncaught exception because session is not a method on the ns object |
@othiym23 this looks like a great module, thanks for all your work! I completely agree with you, that node needs some sort of capability for thread-local storage and that passing request/response objects through your entire application can't be the solution... @nasooz, like you I am using this module in combination with both express and express-session and ran into the same problems with the CLS middleware not carrying through the entire request. You mentioned your workaround with adding the CLS middleware after the session middleware worked for you. Would you be willing to share a bit more of the code that worked for you? I am basically using with the same setup as the code in your first comment, except my CLS middleware is added after the session middleware. I then have another middleware that adds some property from the request object onto the active namespace ( Any suggestions would be greatly appreciated. Thanks! |
This is an abbreviated chunk of a larger solution (e.g. you need to set up domains earlier on in execution) -- but I thought I would share nonetheless. Pretty sure we've nailed the issue at least for our case. Will possibly add the other code needed later. Also see my comment here: nodejs/node#66 var domain = require("domain")
var session = require('express-session');
var sessionMiddleware = exports.sessionMiddleware = session({
secret: secret,
store: sessionMongoStore,
rolling:true
})
function domainBind(fn)
{
if( process.domain && typeof fn === 'function'){
fn = process.domain.bind(fn)
}
return fn
}
app.use(function(req, res, next)
{
var myNext = domainBind(function(){
next();
})
sessionMiddleWare(req,res, myNext)
}); |
Just to say that I don't think this is a bug with CLS. Just But I think the following should work... Replace: app.use( session( { /* options */ } ) ); with: var sessionMiddleware = session( { /* options */ } );
app.use( function(req, res, next) {
return sessionMiddleware.call( this, req, res, ns.bind(next) );
} ); |
A function which patches an express middleware to make it retain CLS context: function clsifyMiddleware(fn, ns) {
return function(req, res, next) {
return fn.call(this, req, res, ns.bind(next));
};
} Then you do: app.use( clsifyMiddleware( session( { /* options */ } ), ns ) ); NB the above doesn't re-bind |
Hi Forrest,
First of all, in my view cls is a wonderful peace of software. I just started using it and it solves lots of issues for me. Thank you for creating it and sharing it.
This is actually a questing rather than an issue. I've successfully integrated cls in an Express 4 application but after adding session middleware I started experiencing issues. To be more specific, the context gets lost. The code I've got is very similar to the following:
As per the comments in the code above, the content gets lost for all middleware after
app.use(session());
I went through all open and closed issues and I gather that in this kind of situations ns.bind() can be used but I don't quite understand how. So, I was wandering if you could give me some directions as to how to approach and solve this issue?
Thank you in advance.
I've got one other minor note. In the very first code example in your README.md, you have
session.set('user', user);
which doesn't seem to be enclosed in asession.run()
. Is this OK or my understanding of cls is still too shallow?Cheers,
Nasko.
The text was updated successfully, but these errors were encountered: