Robert M Robert M - 11 days ago 8
Node.js Question

Node automatically makes session variables?

I am using express with the express-session package and can successfully keep a session variable like such,

router.get('/test', function(req, res, next){
req.session.test = 'awesome';
res.send('awesome')
})


and when I use the code below I will get the desired result

router.get('/pest', function(req, res, next){

res.send(req.session.test)
})


I do not have an issue, but I am having trouble understanding, does Node automatically create sessions with other variables? for example:

router.get('/test', function(req, res, next){
router.test = 'test';
res.send('test')
})


then going to

router.get('/lest', function(req, res, next){
res.send(router.test)
})


makes the same exact result. It seems odd that node will automatically keep the variable like that after some time.

Answer

It isn't a session variable.

You have a single router object.

Modifications to it will persist for every request, not just those that come from a browser with a given session cookie.