Reputation: 2290
server.js
var Session = require('continuation-local-storage').createNamespace('session')
app.use(function (req, res, next) {
// create a new context and store request object
Session.run(function() {
Session.set('req', req);
next()
})
});
other-module.js
var Session = require('continuation-local-storage').getNamespace('session')
Session.get('req') // returns 'undefined'
How to get data from continuation-local-storage when to context is already not active?
Upvotes: 1
Views: 1758
Reputation: 1642
I know it's been long time but I am adding the answer for the benefit of people coming accross this question.
This is the way I am using it:
to set
var session = require('continuation-local-storage').createNamespace('session')
app.use(function(req, res, next) {
session.bindEmitter(req);
session.bindEmitter(res);
session.run(function() {
session.set('req', req);
next();
});
});
to get is the same as in the question
var session = require('continuation-local-storage').getNamespace('session')
session.get('req')
Upvotes: 4