I am having a hard time understanding the role of the cookie that is generated in the express-session
package.
So in the code below:
var session = require('express-session')
var store = require('session-file-store')(session)
app.use(session({
name: 'session-id',
secret: 'sdafjlkdashf34khrjke'
saveUninitialized: true,
resave: true,
store: new Store()
})
func auth (req, res, next) {
if (!req.session.user) {
req.session.user = 'admin'
next()
}else {
console.log(req.session)
}
}
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
So essentially in the simple code snippet above, whenever the user makes a request to the server (like this: localhost:3000/
) 3 things will happen:
- A new session object will be created for that specific client.
- In that new session object a cookie will be attached automatically for that specific client.
- Finally since I am using session-file-store, ONLY the cookie information (not the session object) will get saved to a
session
folder on my local machine.
So now let's say 50 cookie objects are stored in that session
folder since 50 clients made a request to my server.
So my question is, whenever one of those 50 users makes another request to my server, how does express-session
know which cookie object (from the session
folder) to pick and attach to the req
object to use in the code.