vendredi 17 avril 2015

How to create a global per session variable in Node JS

I know doing things such as



myvar=var Math.Sqrt(25);


will create a per user variable rather than doing something like



var myvar=Math.Sqrt(25);


My use case is having a variable stored with a user's session and only for that user. My app works correctly if I use app.locals.myVar, but that is per server rather than perclient. I've tried to use express-session, but for some reason after I initialize my session I cannot create variables.


inside app.js



var session = require('express-session');
app.use(session({secret: 'qwerty'}));


....



app.get('/search', function (req, res){
var searchParameters = req.query.searchInput;
var cook="";
sess=req.session;
sess.testVar=null;


my code will error on the last line where I try to assign my testVar.


Aucun commentaire:

Enregistrer un commentaire