API Reference

cookie-parser

Express middleware that parses Cookie headers into req.cookies and verifies signed cookies.

Official Documentation ↗
CompatibilityThis documentation covers version cookie-parser v1.4.x / Express 4.x / Node 18 LTS. Check the official docs for the latest API surface.

Installation

npmnpm install cookie-parser
pnpmpnpm add cookie-parser
yarnyarn add cookie-parser

Methods

Overview

cookie-parser reads the Cookie header into convenient objects and verifies signatures for signed cookies. It does not encrypt or validate content, so trust decisions remain the application responsibility.

Core Methods

Apply the middleware with an optional secret, then read req.cookies for plain values and req.signedCookies for verified ones. Signed values are produced with res.cookie when signing is configured.

Usage Notes

Treat all cookie data as untrusted input, keep secrets out of payloads, and set HttpOnly, Secure and SameSite attributes on sensitive cookies. Prefer server-side sessions.

Common Pitfalls

Assuming req.cookies is safe leads to trust bugs. Signed does not mean encrypted. Third-party cookie restrictions and proxy stripping cause missing cookies that look like code bugs.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/expressjs/cookie-parser@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:49:01.136Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

Security notes

Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://cdn.jsdelivr.net/gh/expressjs/cookie-parser@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:49:01.136Z.

FAQ

Are parsed cookies trusted?
No. Parsing only decodes values; clients can send anything. Validate and authorize cookie content, and store server-side session state rather than trusting a client value.
How do signed cookies help?
Signing lets the server detect tampering, so signedCookies contains only values whose signature matched. It is not encryption, so do not put secrets in the payload.
Why is req.cookies empty?
The middleware must run before the route and the Cookie header must be present. Proxies can strip cookies, and cross-site cookies may be blocked by SameSite or third-party cookie policies.