Cookies let a server store small pieces of data on the client that are automatically sent back on every subsequent request, commonly used for sessions and preferences. This lesson covers reading and writing cookies in Express.
Reading and Setting Cookies
Express can set cookies out of the box with res.cookie(), but reading them back requires the cookie-parser middleware, without it, req.cookies is undefined.
Cookies sent to the browser support several important options: httpOnly (inaccessible to JavaScript, protecting against XSS theft), secure (only sent over HTTPS), and sameSite (controls cross-site sending behavior).
maxAge is in milliseconds; here the cookie expires after 24 hours (86400000 ms).
res.cookie() Options
res.cookie(name, value, {
httpOnly: true, // not accessible via document.cookie
secure: true, // only sent over HTTPS
sameSite: 'lax', // 'strict' | 'lax' | 'none'
maxAge: 86400000, // lifetime in milliseconds
signed: true, // requires cookie-parser secret
});
httpOnly: true should be set on any cookie storing sensitive session data.
secure: true must be used in production, requiring HTTPS.
sameSite: 'strict' or 'lax' helps mitigate CSRF attacks.
Signed cookies detect tampering, but do not encrypt the value, never store secrets directly in a cookie.
Cookies Cheatsheet
Common cookie operations and options in Express.
Task
Code
Install parser
npm install cookie-parser
Register parser
app.use(cookieParser())
Set a cookie
res.cookie('token', value, { httpOnly: true })
Read cookies
req.cookies.token
Signed cookie
cookieParser('secret') + { signed: true }
Clear a cookie
res.clearCookie('token')
Signed Cookies
Signed cookies add a cryptographic signature so the server can detect if a client tampered with the value, without needing to encrypt it. Pass a secret string into cookieParser() and set signed: true when setting the cookie.
Signed cookies appear on req.signedCookies, not req.cookies.
Clearing Cookies
To log a user out or reset a preference, clear the cookie with res.clearCookie(), passing the same options (path, domain) used when it was originally set.
Storing sensitive data (like a raw password or full user object) in an unsigned, non-httpOnly cookie.
Forgetting secure: true in production, allowing cookies to be sent over plain HTTP.
Reading req.cookies without registering cookie-parser first.
Trying to clear a cookie without matching the original path/domain options, silently failing to remove it.
Key Takeaways
cookie-parser is required to read cookies via req.cookies; setting them works out of the box.
httpOnly, secure, and sameSite are essential security options for any sensitive cookie.
Signed cookies detect tampering but do not hide or encrypt the underlying value.
res.clearCookie() removes a cookie, matching the options it was originally set with.
Pro Tip
Treat cookies as public, tamper-detectable storage at best, never store secrets or sensitive personal data directly in a cookie value, even when signed.
You now know how to work with cookies in Express. Next, learn general input validation strategies in the Validation lesson.