Express session with different cookie domain per request?
Here's what you do:
- write a middleware your app can use in place of the default express.session middleware
- in that middleware, based on the
host
request header instatiate and configure on instance of the express session middleware per domain, and then actually execute the middleware function appropriate for this request
pseudocode
var mwCache = Object.create(null);function virtualHostSession(req, res, next) { var host = req.get('host'); //maybe normalize with toLowerCase etc var hostSession = mwCache[host]; if (!hostSession) { hostSession = mwCache[host] = express.session(..config for this host...); } hostSession(req, res, next); //don't need to call next since hostSession will do it for you}app.use(virtualHostSession);
My requests are highly asynchronous and if I just set it for the whole app at every request, I fear it might not work when two calls come in at once.
Absolutely you cannot do that. It will be utterly incorrect.