Skip to content
Learn Netverks

Lesson

Step 27/36 75% through track

cookies-sessions-concept

Cookies and sessions (concept)

Last reviewed May 28, 2026 Content v20260528
Track mode
nodejs_server
Means
Node sandbox
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Cookies and sessions (concept): the syntax, APIs, and habits you need before advancing in Node.js.

Stateful web apps still rely on sessions and cookies—misuse causes auth bugs and security findings.

You will apply Cookies and sessions (concept) in contexts like: REST/GraphQL APIs, BFF layers, CLIs, webhooks, and real-time services (with WebSockets).

Run JavaScript on the Node runner when configured—never mix arbitrary shell commands in lessons.

When you can explain the previous lesson's ideas without copying starter code.

HTTP is stateless—sessions and cookies remember users across requests. Node APIs often use JWT tokens or server-side sessions with express-session.

Session flow

  1. User logs in; server creates session record with random ID
  2. Server sends Set-Cookie: sessionId=... with HttpOnly, Secure, SameSite flags
  3. Browser sends cookie on later requests; server loads session data

JWT overview

Signed token in Authorization header—stateless verification, harder to revoke instantly without blocklists. Good for microservices; sessions simpler for monolith logout.

Cookie flags

  • HttpOnly — JS cannot read (mitigates XSS theft)
  • Secure — HTTPS only
  • SameSite — reduces CSRF risk

Important interview questions and answers

  1. Q: Where store session data?
    A: Server memory (dev), Redis (prod scale), or database—never sensitive data in unsigned client cookies.
  2. Q: JWT in localStorage?
    A: Risky—XSS can steal it; HttpOnly cookies or short-lived tokens with refresh patterns are safer.

Self-check

  1. What does HttpOnly prevent?
  2. Session vs JWT trade-off for logout?

Tip: Store session IDs in HttpOnly cookies; keep session data server-side—never put secrets in JWT payloads without encryption.

Interview prep

JWT vs server session?

JWTs are stateless tokens (verify signature); server sessions store data server-side with a session ID cookie—sessions easier to revoke, JWTs scale without shared store.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • JWT vs session cookie?
  • HttpOnly why?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump