Express JS Assignment- 5
Basic Questions
- Create a route /session-demo that initializes a session with a key username and logs it to prove what a session is.
- Install and configure express-session in a basic Express app.
- Store a value (e.g., username) in a session and retrieve it later.
- Build two routes: /session-set that saves a session value and /cookie-set that saves a cookie, then compare persistence in browser DevTools.
- Set a cookie named theme with value “dark” using res.cookie().
- Read a cookie from the request using req.cookies.
- Delete a cookie using res.clearCookie().
- Create a route /http-only that sets an httpOnly cookie so it’s not accessible via document.cookie.
- Create a route /secure-cookie that sets a secure cookie which only works over HTTPS.
- Create a route /samesite-cookie that sets a cookie with sameSite: ‘strict’.
- Create a route that sets a cookie with an expiry time of 1 hour.
- Create /session-cookie (expires when browser closes) and /persistent-cookie (expires after 1 day) to show their difference.
- Demonstrate how to destroy a session in Express.
- Log the auto-generated session ID of a user inside /session-id.
- Use cookies + sessions to remember a user’s theme preference (dark or light).
- Write a middleware that checks if a session exists before accessing /dashboard.
- Create a route /sensitive that tries to store a password in a cookie, then explain (via console.log) how it exposes data to the client.
- Create a route /remember-me that sets a cookie for persistent login.
- Create a small front‑end page and backend route — store a token once in localStorage and once in a cookie, then print accessibility differences in DevTools.
- Store a temporary cartId in local storage (frontend + backend logic example).
Intermediate Questions
- Configure session cookies with httpOnly and secure flags.
- Use connect-mongo to store sessions in MongoDB instead of memory.
- Create a login system that uses sessions to keep users logged in.
- Implement logout functionality by destroying the session.
- Add session expiry of 15 minutes after login.
- Implement a “Remember Me” feature using cookies for persistent login.
- Write middleware to refresh session expiry on each request (sliding session).
- Demonstrate how to share session data across multiple routes.
- Create /cookie-token saving auth token in httpOnly cookie and /localstorage-token saving it in localStorage, then test accessibility in browser console.
- Store a JWT token inside an httpOnly cookie.
- Create two routes: /cookie-strict that sets a sameSite:’strict’ cookie and /cookie-lax that sets a sameSite:’lax’ cookie, then test cross‑site requests.
- Create a middleware to log all cookies received in a request.
- Implement cookie-based theme switching (dark/light) and persist it.
- Add CSRF protection by checking custom tokens stored in cookies.
- Demonstrate how to handle session data securely in production.
- Store shopping cart items in session and retrieve them across routes.
- Implement a multi-step form using sessions to remember user input.
- Create a demo where a script injected into localStorage reads sensitive data, proving XSS risk, while cookie with httpOnly flag resists this.
- Write code that combines session + cookies for persistent login.
- Build a login route where user credentials are stored in DB but session is used for state.
Advanced Questions
- Build a complete login + logout + persistent session system using express-session + cookies.
- Store sessions in Redis for scalability and integrate with Express.
- Implement secure cookie handling with httpOnly, secure, sameSite=strict in production mode.
- Create middleware that automatically logs out users if session expired or tampered.
- Design a persistent login system with session rotation to prevent fixation attacks.
- Implement CSRF protection using cookies + tokens in Express.
- Build a shopping cart system where cart data persists across sessions using DB + cookies.
- Combine JWT + cookies + sessions for hybrid authentication.
- Securely integrate local storage with backend by encrypting sensitive data.
- Create a mini project:
- Login/Logout
- Session-based persistence
- Cookie-based “Remember Me”
- Local storage for temporary cart
- Security best practices (httpOnly, sameSite, CSRF)