Node.jsInterview preparation
Foundation·Concept·2 min read

How do cookies and sessions work?

Practice this question

Definition:

A cookie is a small piece of browser-managed data sent automatically with matching requests. A server-side session uses an unpredictable identifier, often in a cookie, to look up login state on the server. This lets the server revoke or change that state without placing all session details in the browser.

Key points:

  • A cookie is data the browser stores and sends with matching requests.

  • A server-side session commonly stores login state behind a random cookie ID.

  • Use HttpOnly, Secure and suitable SameSite settings for session cookies.

  • Regenerate session IDs at authentication boundaries, expire inactive sessions and use a shared session store when instances must share logins.

Interview point:

  • HttpOnly stops JavaScript from reading the cookie, but does not by itself stop CSRF or malicious scripts from making authenticated requests.
Adapted from 02_NODE_EXPRESS_HTTP.md · “How do cookies and sessions work?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.