0

I'm just getting started with Node and I'm running into issues setting variables. In the example below, it appears the value of random will be the same for every user because it's being set on app start.

var random = math.Random();

app.post('/api/whatever/', function(req, res) {
     console.log(random);
});

Using a local variable will mean that the value of random changes every time a post request is made:

app.post('/api/whatever/', function(req, res) {
    var random = math.Random();
    console.log(random);
});

What I'm trying to figure out is how to set the value of random once per user / session (i.e. the first time a user makes a call to /api/whatever). I have other configuration variables to set after the first request, so it may be better to just trigger an init() function. The problem would be the same though.

krazy4code
  • 11
  • 1

2 Answers2

0

Your global variable could hold an object whose keys are session ID and values are whatever session-specific information you want - such as your random number.

To get the session ID, see this question: How can I find the session Id when using express / connect and a session store?

Community
  • 1
  • 1
Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
  • Are you referring to using express-session? I tried that yesterday, but it appears that all requests have the same ID because app.use(session(sess)) is outside my API method (I assume it's set when I start the app using 'node app.js' and not when a user makes a request) I'm just using Postman to test, but when I opened up two tabs, the session id's were the same. – krazy4code Nov 15 '16 at 19:24
0

Use the express-session module:

npm install express-session

Here's an example:

...

var session = require('express-session');

app.use(session({secret: '1234567890QWERTY'}));

app.get('/api/whatever/', function(req, res) {

  if(req.session.random) {
    res.send('Your random key is: ' + req.session.random);

  } else {
    req.session.random = Math.random();
    // Set anything else you want in req.session ...
  }

});
karliwson
  • 3,365
  • 1
  • 24
  • 46
  • I've tested for awhile today. The issue seems to be the same regardless of my method. Global vars don't work because all users will have the same value. Local vars don't work because the value will be updated every time they make a new post call. Even Express-Session will generate a new ID. – krazy4code Nov 16 '16 at 02:14
  • I don't understand. The express session is based on cookies that are specific for each and every user. Every user will have its own session object, where you can set specific user properties.It's not about global or local variables anymore. Can you update your question with the code you've tested? It's really useful to update the question with everhing you've tried so far. – karliwson Nov 16 '16 at 02:31
  • My mistake. Your example works perfectly. The only part I don't understand is how I would get and set session cookie values from a function that lives outside the scope of the above get method. For example, if I called function foo (someparam) {} within app.get and tried to access the session cookie from within that function. – krazy4code Nov 17 '16 at 13:55
  • Can't you pass the cookie value as a parameter to your function? If you want to set cookies, your function can return the value and you use it to set the cookie. Or even better, you pass the `req.session` object to your function, so you will be able to get and set cookies. – karliwson Nov 17 '16 at 14:01