📖 Scenario: You are building a simple Express server that reads cookies sent by the browser. Cookies help remember user preferences or login info.
🎯 Goal: Create an Express app that uses cookie-parser middleware to read cookies from incoming requests and display them on the homepage.
📋 What You'll Learn
Create an Express app with express()
Use cookie-parser middleware
Read cookies from req.cookies
Send the cookies as a JSON response on the root route /
💡 Why This Matters
🌍 Real World
Web servers often need to read cookies to remember user sessions, preferences, or login states.
💼 Career
Understanding how to use cookie-parser and <code>req.cookies</code> is essential for backend developers working with Express to manage user data securely and efficiently.
Progress0 / 4 steps
1
Setup Express app and import cookie-parser
Write code to import express and cookie-parser modules, then create an Express app called app.
Express
Hint
Use require('express') and require('cookie-parser') to import modules. Then call express() to create the app.
2
Add cookie-parser middleware
Add the cookie-parser middleware to the Express app by calling app.use(cookieParser()).
Express
Hint
Use app.use(cookieParser()) to enable cookie parsing for all routes.
3
Create a route to read cookies
Create a GET route for '/' that reads cookies from req.cookies and sends them as JSON using res.json(req.cookies).
Express
Hint
Use app.get('/', (req, res) => { ... }) and inside send req.cookies as JSON.
4
Start the server on port 3000
Add code to start the Express server on port 3000 using app.listen(3000).
Express
Hint
Use app.listen(3000) to start the server on port 3000.
Practice
(1/5)
1. What does req.cookies represent in an Express app when using cookie-parser middleware?
easy
A. A function to set cookies on the response
B. An object containing cookies sent by the browser
C. A string of all cookies concatenated
D. An array of cookie names only
Solution
Step 1: Understand the role of cookie-parser
The cookie-parser middleware parses cookies from the HTTP request headers.
Step 2: Identify what req.cookies holds
After parsing, req.cookies is an object where each cookie name is a key and its value is the cookie's value.
Final Answer:
An object containing cookies sent by the browser -> Option B
Quick Check:
req.cookies = object of cookies [OK]
Hint: Remember: req.cookies is an object, not a string or function [OK]
Common Mistakes:
Thinking req.cookies is a function
Expecting req.cookies to be a string
Confusing req.cookies with response cookies
2. Which of the following is the correct way to use cookie-parser middleware in an Express app?
easy
A. app.use(cookieParser);
B. app.cookieParser();
C. app.use(cookieParser());
D. ;)(resraPeikooc.ppa
Solution
Step 1: Recall middleware usage in Express
Middleware functions are added using app.use() and must be called as functions if they return middleware.
Step 2: Apply this to cookie-parser
The correct syntax is app.use(cookieParser()); to invoke the function and add the middleware.
Final Answer:
app.use(cookieParser()); -> Option C
Quick Check:
Middleware added with app.use(cookieParser()) [OK]
Hint: Use parentheses to call middleware function inside app.use [OK]
Common Mistakes:
Forgetting parentheses after cookieParser
Calling cookieParser as a method on app
Passing cookieParser without calling it
3. Given this Express route with cookie-parser middleware, what will be the output if the browser sends a cookie named user with value alice?
A. cookie-parser middleware is added after the route, so req.cookies is undefined
B. cookie-parser is not imported correctly
C. The route handler should use res.cookies instead
D. Express does not support cookies natively
Solution
Step 1: Check middleware order
Middleware must be added before routes to affect req objects in those routes.
Step 2: Identify the problem in code
Here, app.use(cookieParser()) is after the route, so req.cookies is not set when the route runs.
Final Answer:
cookie-parser middleware is added after the route, so req.cookies is undefined -> Option A
Quick Check:
Middleware order matters = cookie-parser middleware is added after the route, so req.cookies is undefined [OK]
Hint: Add middleware before routes to access req.cookies [OK]
Common Mistakes:
Placing middleware after routes
Confusing req.cookies with res.cookies
Not calling cookieParser as a function
5. You want to create an Express route that reads a cookie named theme and sends back 'Dark mode' if its value is 'dark', otherwise 'Light mode'. Which code correctly implements this using req.cookies with cookie-parser?
hard
A. app.get('/mode', (req, res) => {
if (req.cookies.theme === 'dark') res.send('Dark mode');
else res.send('Light mode');
});
B. app.get('/mode', (req, res) => {
if (req.cookies['theme'] = 'dark') res.send('Dark mode');
else res.send('Light mode');
});
C. app.get('/mode', (req, res) => {
if (req.cookies.theme == dark) res.send('Dark mode');
else res.send('Light mode');
});
D. app.get('/mode', (req, res) => {
if (req.cookies.theme === dark) res.send('Dark mode');
else res.send('Light mode');
});
Solution
Step 1: Check cookie access and comparison
Use strict equality === to compare req.cookies.theme to the string 'dark'.
Step 2: Identify correct syntax
app.get('/mode', (req, res) => {
if (req.cookies.theme === 'dark') res.send('Dark mode');
else res.send('Light mode');
}); uses === and string 'dark' correctly. app.get('/mode', (req, res) => {
if (req.cookies['theme'] = 'dark') res.send('Dark mode');
else res.send('Light mode');
}); uses assignment = instead of comparison. app.get('/mode', (req, res) => {
if (req.cookies.theme == dark) res.send('Dark mode');
else res.send('Light mode');
}); uses loose equality == with unquoted dark. app.get('/mode', (req, res) => {
if (req.cookies.theme === dark) res.send('Dark mode');
else res.send('Light mode');
}); uses undefined variable dark without quotes.
Final Answer:
The code using strict equality req.cookies.theme === 'dark' -> Option A
Quick Check:
Use === 'dark' to compare cookie value [OK]
Hint: Use === and quotes for string comparison with cookie values [OK]
Common Mistakes:
Using = instead of === for comparison
Not quoting string literals
Using loose equality == instead of ===
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.