D. Assigning session to 'res.session' instead of 'req.session'
Solution
Step 1: Check session assignment
Session data should be stored on req.session, not res.session.
Step 2: Confirm correct session usage
Using res.session will cause undefined error; req.session is correct.
Final Answer:
Assigning session to 'res.session' instead of 'req.session' -> Option D
Quick Check:
Session stored on req, not res [OK]
Hint: Session is on req, not res object [OK]
Common Mistakes:
Confusing req and res objects
Ignoring missing status code on failure
Thinking '==' causes error here
5. You want to keep users logged in across pages after login in Express. Which approach correctly implements this using sessions?
1. Use express-session middleware
2. On successful login, save username in req.session
3. On other routes, check if req.session.user exists
4. If exists, allow access; else redirect to login
hard
A. Sessions should not be used; use cookies only
B. This approach is correct and follows best practices
C. Store user info in res.locals instead of session
D. Use GET method to store session data
Solution
Step 1: Understand session usage in Express
express-session middleware manages sessions; storing user info in req.session keeps login state.
Step 2: Verify access control logic
Checking req.session.user on other routes to allow or redirect is standard practice.
Final Answer:
This approach is correct and follows best practices -> Option B