res.cookie() sets a cookie by writing a Set-Cookie header on the response. The browser stores the cookie; Express only sends the header. The value may be a string or an object (serialized as JSON).
Use options for path, expiry, and security (httpOnly, secure, sameSite). Call it more than once to set several cookies. To remove one later, use res.clearCookie() with matching options. Read cookies on the next request via req.cookies (with cookie-parser).
Syntax
Call res.cookie in your Express.js app. Start with this line.
POST /login
→ Set-Cookie: token=abc123; Max-Age=3600; Path=/; HttpOnly; Secure; SameSite=Lax
→ 200 { "ok": true }
(On plain HTTP, Secure cookies may not be stored — use HTTPS in production.)
How It Works
httpOnly. Blocks access from document.cookie (XSS mitigation).
secure. Cookie is sent only on HTTPS.
sameSite: 'lax'. Limits when the cookie is included on cross-site requests.
maxAge. Convenience for expiry relative to now (here, one hour).
Example 3 — Signed Cookie with cookie-parser
Sign a value so tampering is detectable. Read it later from req.signedCookies.
GET /remember
→ Set-Cookie: userPref=s%3Acompact.<signature>; Max-Age=86400; Path=/
→ Body: Signed cookie set
GET /pref (same browser, cookie present)
→ 200 { "userPref": "compact" }
How It Works
Mount a secret.cookieParser('my-secret') enables signing and parsing.
signed: true. Express signs the value with that secret.
Read from signedCookies. Valid values appear on req.signedCookies; tampered ones do not.
Unsigned still possible. Plain cookies stay on req.cookies when you omit signed.
Compatibility
Runtime Support
res.cookie() is an Express.js response method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers store the cookie after receiving Set-Cookie; they do not execute this method.
✓ Server · Express 4 & 5
res.cookie()
Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.
ServerNode.js runtime API
Node.js18+ for Express 5 · Express 4 supported
Supported
BunExpress-compatible server apps
Supported
DenoVia npm compatibility / adapters
Supported
res.cookie()Express on Node.js
Bottom line: Study and run these examples with Node.js and the express package. Add cookie-parser when reading or signing cookies.
Frequently Asked Questions
It sets a cookie by adding a Set-Cookie header to the response. It does not store cookies on the server; the browser stores them after receiving the header.
Use maxAge (milliseconds) or expires (a Date). maxAge is a convenience for relative expiry. To remove a cookie, call res.clearCookie() with matching options.
httpOnly blocks document.cookie access. secure sends the cookie only over HTTPS. sameSite controls cross-site sending (for example lax or strict) and helps reduce CSRF risk.
With cookie-parser(secret) mounted, pass { signed: true }. Express signs the value; read it later from req.signedCookies.
Yes. Objects are serialized as JSON in the cookie value. Keep cookie payloads small.