I am very new to learning Node and Express, and I'm still trying to wrap my head in code movement with an expression. Suppose we have code that looks like this in session.js:
app.post('/session', notLoggedIn, function(req, res) { User.findOne({ username: req.body.username, password: req.body.password }, function (err, user) { if (err) { return next(err); } if (user) { req.session.user = user; res.redirect('/users'); } else { res.redirect('/session/new'); } }); });
Assuming the user is a mandatory mongo scheme. What I find strange is the appointment of session.user:
req.session.user = user;
Since the req variable will be unavailable after the redirect, but we obviously do this to save user data, I can only figure out which of the following scripts describes what happens. Either (A) the argument that is assigned to the req parameter (when the callback is called) is stored / somewhere else on the stack, (B) the session is stored / is on the stack and assigned to the new req object before it (C) matches B, but in the userβs field (it seems unlikely and possibly far-fetched on my part).
Nicholasfolk
source share