0
0
Expressframework~10 mins

req.cookies with cookie-parser in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - req.cookies with cookie-parser
Client sends HTTP request with cookies
Express server receives request
cookie-parser middleware runs
Parse cookies from request headers
Attach parsed cookies to req.cookies
Route handler accesses req.cookies
Respond based on cookie values
This flow shows how Express uses cookie-parser middleware to read cookies from incoming requests and makes them available as req.cookies for route handlers.
Execution Sample
Express
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
app.use(cookieParser());
app.get('/', (req, res) => {
  res.send(req.cookies);
});
This code sets up an Express server that uses cookie-parser to read cookies and sends them back in the response.
Execution Table
StepActionRequest Headersreq.cookiesResponse Sent
1Client sends request with header 'Cookie: user=alice; theme=dark'Cookie: user=alice; theme=dark{}N/A
2Express receives requestCookie: user=alice; theme=dark{}N/A
3cookie-parser middleware parses cookiesCookie: user=alice; theme=dark{"user":"alice","theme":"dark"}N/A
4Route handler reads req.cookiesCookie: user=alice; theme=dark{"user":"alice","theme":"dark"}N/A
5Route handler sends req.cookies as responseCookie: user=alice; theme=dark{"user":"alice","theme":"dark"}{"user":"alice","theme":"dark"}
6Request handling completeCookie: user=alice; theme=dark{"user":"alice","theme":"dark"}Response sent with cookies object
💡 Request handled and response sent with parsed cookies from client
Variable Tracker
VariableStartAfter Step 3After Step 4Final
req.cookies{}{"user":"alice","theme":"dark"}{"user":"alice","theme":"dark"}{"user":"alice","theme":"dark"}
Key Moments - 2 Insights
Why is req.cookies empty before cookie-parser runs?
Because Express does not parse cookies by default. cookie-parser middleware reads the Cookie header and fills req.cookies, as shown in step 3 of the execution_table.
What happens if the client sends no cookies?
req.cookies will be an empty object {}, since cookie-parser finds no cookies to parse. This is implied by the initial empty state in the variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does req.cookies contain?
A{}
B{"user":"alice","theme":"dark"}
Cundefined
Dnull
💡 Hint
Check the req.cookies column at step 3 in the execution_table
At which step does the route handler send the response?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the Response Sent column in the execution_table
If the client sends no Cookie header, how would req.cookies look after middleware?
AAn empty object {}
Bnull
Cundefined
DAn error is thrown
💡 Hint
Refer to the key_moments explanation about no cookies sent
Concept Snapshot
Use cookie-parser middleware in Express to parse cookies from incoming requests.
Add cookie-parser with app.use(cookieParser()).
Access cookies via req.cookies as an object.
If no cookies sent, req.cookies is {}.
Cookies come from the HTTP Cookie header.
Route handlers can read req.cookies to customize responses.
Full Transcript
When a client sends an HTTP request with cookies, Express receives it. The cookie-parser middleware runs first, reading the Cookie header from the request. It parses the cookies into a JavaScript object and attaches it to req.cookies. Then, route handlers can access req.cookies to read cookie values. If no cookies are sent, req.cookies is an empty object. Finally, the route handler can send a response using the cookie data. This process allows easy access to cookies in Express apps.