Express.js res.cookie() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Response

Definition

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.

Express
res.cookie(name, value [, options])
res.cookie('name', 'tobi', { domain: '.example.com', path: '/admin', secure: true });
res.cookie('remember_me', '1', { maxAge: 900000, httpOnly: true });
res.cookie('cart', { items: [1, 2, 3] });
CallWhat it does
res.cookie(name, value [, options])Primary signature.
res.cookie('name', 'tobi', { domain: '.example.com', path: '/admin', secure: true });Related call form.
res.cookie('remember_me', '1', { maxAge: 900000, httpOnly: true });Related call form.
res.cookie('cart', { items: [1, 2, 3] });Related call form.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

res.cookie(name, value [, options])
res.cookie('name', 'tobi', { domain: '.example.com', path: '/admin', secure: true });
res.cookie('remember_me', '1', { maxAge: 900000, httpOnly: true });
res.cookie('cart', { items: [1, 2, 3] });

Parameters

ParameterDescription
nameRequired. Cookie name.
valueRequired. Cookie value. Objects are JSON-serialized.
optionsOptional. Attributes for the Set-Cookie header (see table below).
pathRequired. Default "/". Must match later on clearCookie.
domainRequired. Host scope for the cookie.
maxAgeRequired. Lifetime in milliseconds from now (sets Expires).
expiresRequired. Absolute expiry. Omit or 0 → session cookie.
httpOnlyRequired. Not readable from document.cookie.
secureRequired. Send only over HTTPS.
sameSiteRequired. Cross-site policy (lax, strict, none, …).
signedRequired. Sign with cookie-parser(secret); read via req.signedCookies.
encodeRequired. Sync encoder; default encodeURIComponent.

Return Value

res.cookie() returns res for chaining. It queues a Set-Cookie header — nothing is stored in your Node process unless you also save data yourself.

Express
res.cookie('theme', 'dark', { maxAge: 86400000, httpOnly: true })
  .send('ok');

Express.js Examples

Example 1 — Set a Simple Cookie

Store a preference with default path /.

Express
const express = require('express');
const app = express();

app.get('/set-theme', (req, res) => {
  res.cookie('theme', 'dark');
  res.send('Theme cookie set');
});

app.listen(3000);

How It Works

  1. Name and value. res.cookie('theme', 'dark') queues a Set-Cookie header.
  2. Default path is /. Omitted options follow RFC defaults.
  3. No expiry → session cookie. It lasts until the browser session ends (unless you set maxAge/expires).
  4. Then send the body. Headers go out with res.send.

Example 2 — Secure Auth Cookie with Options

Set a short-lived token cookie that JavaScript cannot read.

Express
const express = require('express');
const app = express();

app.post('/login', (req, res) => {
  res.cookie('token', 'abc123', {
    httpOnly: true,
    secure: true,
    sameSite: 'lax',
    maxAge: 60 * 60 * 1000 // 1 hour
  });
  res.json({ ok: true });
});

app.listen(3000);

How It Works

  1. httpOnly. Blocks access from document.cookie (XSS mitigation).
  2. secure. Cookie is sent only on HTTPS.
  3. sameSite: 'lax'. Limits when the cookie is included on cross-site requests.
  4. 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.

Express
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();

app.use(cookieParser('my-secret'));

app.get('/remember', (req, res) => {
  res.cookie('userPref', 'compact', {
    signed: true,
    maxAge: 86400000
  });
  res.send('Signed cookie set');
});

app.get('/pref', (req, res) => {
  res.json({ userPref: req.signedCookies.userPref });
});

app.listen(3000);

How It Works

  1. Mount a secret. cookieParser('my-secret') enables signing and parsing.
  2. signed: true. Express signs the value with that secret.
  3. Read from signedCookies. Valid values appear on req.signedCookies; tampered ones do not.
  4. Unsigned still possible. Plain cookies stay on req.cookies when you omit signed.

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.

Server Node.js runtime API
Node.js 18+ for Express 5 · Express 4 supported
Supported
Bun Express-compatible server apps
Supported
Deno Via 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.

Next: res.download()

Transfer a file from disk as a browser download.

res.download() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful