简体   繁体   中英

User specific global variable in Node.js

my program looks as follows:

var globVar=[];

function1(usr){
   //calculations with globVar
}

function2(usr){
   //calculations with globVar
}

app.post('/', function(req, res) {
    var formString = req.body.filter1;
    globVar.push(formString);
    usr= req.connection.user;
    function1(usr);
    function2(usr);
    res.redirect('/');
});

req.connection.user comes from nodeSSPI module. I need to make this globVar user specific so it doesn't mixed up when users runs this app concurrently. Now it works well but only for single user:)

Thank you for your consideration on this matter.

I've been looking at what seems like the same problem and my solution was to simply use cookies, and set a variable within the cookies client-side.

In my case the problem was setting language choice permanently for each user and that was simplest.

This guide helps: https://www.tutorialspoint.com/expressjs/expressjs_cookies.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM