cookie-parser: From Header String to Usable Object

The cookie-parser middleware translates the raw Cookie header string into a usable req.cookies object. It's used in Express apps to read session IDs or user preferences.
Why it exists
Raw HTTP requests send all cookies as a single, semi-colon delimited string in the Cookie header. Manually parsing this string in every request handler is repetitive and error-prone. cookie-parser was created to centralize this parsing logic into a reusable Express middleware.
The mental model
Think of cookie-parser as a pre-processor for your Express routes. It intercepts each incoming request, finds the raw Cookie header string, translates it into a clean JavaScript object, and attaches it to the request as req.cookies before your own code runs.
How it works
When you add app.use(cookieParser()), the middleware runs on every request. It reads req.headers.cookie, splits it into key-value pairs, and populates req.cookies. If you provide a secret, like app.use(cookieParser('a-secret-string')), it enables signed cookie support. It looks for cookies prefixed with s:, validates their signature against the secret, and if valid, moves them into a separate req.signedCookies object. A cookie that fails this signature validation will have its value set to false in req.signedCookies, indicating tampering. The middleware also supports "JSON cookies" prefixed with j:, automatically parsing them with JSON.parse.
When to use it
Use this middleware in any Express app where you need to read data from cookies. This is fundamental for managing user sessions, storing preferences, or handling authentication tokens. It is a standard component for most stateful web applications built with Express.
When not to use it
Since it's no longer bundled with Express, you must install it explicitly. Some higher-level libraries, like express-session, handle cookie parsing internally. If your only use for cookies is for such a library, you might not need to install and use cookie-parser directly. Always check the dependency's documentation.
One canonical example
After setting up Express and adding app.use(cookieParser('my-secret')), you can set a signed cookie in one route with res.cookie('user', 'admin', { signed: true }). In another route, you can then access this value. The regular req.cookies object will not contain this cookie, but the req.signedCookies object will contain { user: 'admin' }. If a client had tampered with this cookie, req.signedCookies.user would be false.
Interview question
When cookie-parser with a secret detects a tampered signed cookie, what happens?
- a.An unhandled exception is thrown, stopping further middleware execution.
- b.The tampered cookie is entirely omitted from both req.cookies and req.signedCookies.
- c.The request is automatically terminated with a 403 Forbidden error.
- d.The cookie's value is set to false within the req.signedCookies object.Correct
Why? this is the answer
The card explicitly states that a cookie failing signature validation will have its value set to false in req.signedCookies. This allows the application to detect tampering and decide how to proceed, rather than cookie-parser automatically rejecting the request or throwing an error.
Just read this? Test yourself on what you have been reading.
Read the original → expressjs.com
- #express
- #nodejs
- #middleware
- #cookies
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on express — each one lists the topics its interview covers.
See open roles