cookie-parser
Express middleware that parses Cookie headers into req.cookies and verifies signed cookies.
Official Documentation ↗cookie-parser v1.4.x / Express 4.x / Node 18 LTS. Check the official docs for the latest API surface.Installation
npm install cookie-parserpnpm add cookie-parseryarn add cookie-parserMethods
No methods match this filter.
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.
- Signed cookies that fail signature validation will have the value false instead of the tampered value. (https://cdn.jsdelivr.net/gh/expressjs/cookie-parser@latest/README.md)
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.
- Optionally you may enable signed cookie support by passing a secret string, which assigns req.secret so it may be used by other middleware. (https://cdn.jsdelivr.net/gh/expressjs/cookie-parser@latest/README.md)
- If an array is provided, an attempt will be made to unsign the cookie with each secret in order. (https://cdn.jsdelivr.net/gh/expressjs/cookie-parser@latest/README.md)
- When secret is provided, this module will unsign and validate any signed cookie values and move those name value pairs from req.cookies into req.signedCookies. (https://cdn.jsdelivr.net/gh/expressjs/cookie-parser@latest/README.md)
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.
Report a problem with this API docOpens a pre-filled issue on GitHub.