Express.js req.cookies Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Request

Definition

In Express.js, req.cookies is an object of cookie name/value pairs from the request Cookie header. With the cookie-parser middleware mounted, a header like Cookie: name=tj becomes req.cookies.name === 'tj'.

If the client sends no cookies, it defaults to {}. Signed cookies belong in req.signedCookies instead. This is a server-side property on the request object.

Syntax

Read req.cookies in your Express.js route handler. Start with this line.

Express
req.cookies
req.cookies.cookieName
const cookieParser = require('cookie-parser')
app.use(cookieParser())
app.use(cookieParser('my-secret')) // also enables signedCookies
CallWhat it does
req.cookiesPrimary signature.
req.cookies.cookieNameRelated call form.
const cookieParser = require('cookie-parser')Related call form.
app.use(cookieParser())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();

req.cookies
req.cookies.cookieName
const cookieParser = require('cookie-parser')
app.use(cookieParser())
app.use(cookieParser('my-secret')) // also enables signedCookies

Parameters

ParameterDescription
cookie-parser middlewareRequired. Without it, req.cookies is not populated
Cookie request headerOptional. Browsers send cookies automatically for matching domains
Secret stringOptional. Enables req.signedCookies validation

Return Value

req.cookies is an object of unsigned cookies populated by cookie-parser. Without that middleware, the property is not usefully available.

Express
app.use(cookieParser());
app.get('/', (req, res) => {
  const theme = req.cookies.theme; // string or undefined
});

Express.js Examples

Example 1 — Read Cookies with cookie-parser

Mount the middleware, then read a cookie name (same idea as the official docs).

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

app.use(cookieParser());

app.get('/', (req, res) => {
  // Cookie: name=tj
  console.log(req.cookies.name); // 'tj'
  res.json({ cookies: req.cookies });
});

app.listen(3000);

How It Works

  1. Install and require. cookie-parser is a separate package from Express.
  2. Mount early. app.use(cookieParser()) runs before routes and parses the Cookie header.
  3. Read by name. req.cookies.name is the string value for that cookie.
  4. Empty default. With no cookies sent, req.cookies is {}.

Example 2 — Set and Read a Preference Cookie

Use res.cookie to store a theme, then read it back on the next request via req.cookies.

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

app.use(cookieParser());

app.get('/theme/:value', (req, res) => {
  const theme = req.params.value === 'dark' ? 'dark' : 'light';
  res.cookie('theme', theme, { httpOnly: true, maxAge: 7 * 24 * 60 * 60 * 1000 });
  res.send('Theme saved: ' + theme);
});

app.get('/profile', (req, res) => {
  const theme = req.cookies.theme || 'light';
  res.send('Theme: ' + theme);
});

app.listen(3000);

How It Works

  1. Write with res.cookie. The server sends a Set-Cookie header; the browser stores it.
  2. Limit the value. Only allow dark or light so clients cannot inject arbitrary themes.
  3. Read on later requests. The browser sends Cookie: theme=dark; parser fills req.cookies.theme.
  4. Provide a fallback. || 'light' covers first visits with no cookie yet.

Example 3 — Prefer Signed Cookies for Integrity

Pass a secret to cookieParser. Read sensitive IDs from req.signedCookies, not req.cookies.

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

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

app.get('/login', (req, res) => {
  res.cookie('userId', '42', { signed: true, httpOnly: true });
  res.send('Logged in');
});

app.get('/dashboard', (req, res) => {
  const userId = req.signedCookies.userId;
  if (!userId) {
    return res.status(401).send('Unauthorized');
  }
  res.send('Welcome user ' + userId);
});

app.listen(3000);

How It Works

  1. Share a secret. cookieParser('my-secret-key') enables signing and verification.
  2. Set with signed: true. The cookie value is prefixed and signed; it lands in req.signedCookies later.
  3. Read signed values only. Do not trust req.cookies.userId for this ID — that channel is easy to spoof.
  4. Treat false as failure. A bad signature becomes false; missing cookies are undefined. Both mean “not authorized.”

Runtime Support

req.cookies is an Express.js request property filled by cookie-parser on the server. It runs on Node.js (Express 5 needs Node.js 18+). Browsers only send the Cookie header.

✓ Server · Express 4 & 5

req.cookies

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 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
req.cookies Express on Node.js

Bottom line: Install cookie-parser, mount it with app.use, then read req.cookies in your routes.

Frequently Asked Questions

It is an object of cookie name/value pairs from the request Cookie header. cookie-parser middleware fills it; with no cookies it defaults to {}.
Yes. Install and mount cookie-parser with app.use(cookieParser()) before your routes. Express does not parse cookies by itself.
req.cookies holds unsigned cookies. Signed cookies are moved to req.signedCookies after the secret validates them, so spoofed values are easier to spot.
No. Signing only detects tampering. The value is still readable by the client — never put secrets in cookie values.
No. Clients can forge Cookie headers. Validate values, and use signed cookies when integrity matters.

Next: req.fresh

Check whether the client cache is still fresh for conditional requests.

req.fresh 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