Bird
Raised Fist0
Expressframework~20 mins

req.cookies with cookie-parser in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Cookie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What does req.cookies contain after using cookie-parser?

Consider an Express app using cookie-parser middleware. A client sends a request with the header Cookie: user=alice; theme=dark. What will req.cookies contain inside a route handler?

Express
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
app.use(cookieParser());
app.get('/', (req, res) => {
  res.json(req.cookies);
});
A{"user": "alice", "theme": "dark"}
B["user=alice", "theme=dark"]
Cundefined
D"user=alice; theme=dark"
Attempts:
2 left
💡 Hint

Think about how cookie-parser parses the cookie header into an object.

📝 Syntax
intermediate
2:00remaining
Which code correctly uses cookie-parser to access cookies?

Which of the following Express app snippets correctly sets up cookie-parser and accesses a cookie named sessionId?

A
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
app.use(cookieParser);
app.get('/', (req, res) => {
  res.send(req.cookies.sessionId);
});
B
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
app.get('/', (req, res) => {
  res.send(req.cookies.sessionId);
});
app.use(cookieParser());
C
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
app.use(cookieParser());
app.get('/', (req, res) => {
  res.send(req.cookies.sessionId);
});
D
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
app.use(cookieParser());
app.get('/', (req, res) => {
  res.send(req.cookie.sessionId);
});
Attempts:
2 left
💡 Hint

Remember to call cookieParser() as a function and use req.cookies (plural).

🔧 Debug
advanced
2:00remaining
Why does req.cookies remain empty despite sending cookies?

An Express app uses cookie-parser middleware. The client sends cookies, but req.cookies is always empty. What is the most likely cause?

Express
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
app.get('/', (req, res) => {
  res.json(req.cookies);
});
app.use(cookieParser());
AThe client did not send any cookies in the request headers.
BThe middleware <code>cookie-parser</code> is used after the route, so cookies are not parsed before the route runs.
CThe <code>cookie-parser</code> middleware is missing the secret key argument.
DThe <code>req.cookies</code> property is deprecated and replaced by <code>req.signedCookies</code>.
Attempts:
2 left
💡 Hint

Middleware order matters in Express.

❓ state_output
advanced
2:00remaining
What is the value of req.signedCookies after using cookie-parser('secret')?

Given the Express app below, what will req.signedCookies contain if the client sends a cookie token=abc123 signed with the secret secret?

Express
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
app.use(cookieParser('secret'));
app.get('/', (req, res) => {
  res.json({
    cookies: req.cookies,
    signedCookies: req.signedCookies
  });
});
Aundefined
B{}
Cnull
D{"token": "abc123"}
Attempts:
2 left
💡 Hint

Signed cookies are verified and available in req.signedCookies.

🧠 Conceptual
expert
2:00remaining
Which statement about cookie-parser and req.cookies is TRUE?

Choose the correct statement about how cookie-parser works with req.cookies in Express.

A<code>cookie-parser</code> parses the <code>Cookie</code> header and populates <code>req.cookies</code> as an object with cookie names and values.
B<code>req.cookies</code> is automatically available in Express without any middleware.
C<code>cookie-parser</code> encrypts cookies before sending them to the client.
D<code>req.cookies</code> contains only signed cookies when <code>cookie-parser</code> is used with a secret.
Attempts:
2 left
💡 Hint

Think about what cookie-parser does and what req.cookies holds.

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

  1. Step 1: Understand the role of cookie-parser

    The cookie-parser middleware parses cookies from the HTTP request headers.
  2. 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.
  3. Final Answer:

    An object containing cookies sent by the browser -> Option B
  4. 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

  1. Step 1: Recall middleware usage in Express

    Middleware functions are added using app.use() and must be called as functions if they return middleware.
  2. Step 2: Apply this to cookie-parser

    The correct syntax is app.use(cookieParser()); to invoke the function and add the middleware.
  3. Final Answer:

    app.use(cookieParser()); -> Option C
  4. 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?
app.get('/profile', (req, res) => {
  res.send(`User: ${req.cookies.user}`);
});
medium
A. User: [object Object]
B. User: undefined
C. Error: req.cookies is not defined
D. User: alice

Solution

  1. Step 1: Check if cookie-parser is used

    Assuming cookie-parser middleware is active, req.cookies contains parsed cookies.
  2. Step 2: Access the user cookie value

    The code accesses req.cookies.user, which equals 'alice' from the browser cookie.
  3. Final Answer:

    User: alice -> Option D
  4. Quick Check:

    Cookie value accessed correctly = User: alice [OK]
Hint: Access cookie by name on req.cookies object [OK]
Common Mistakes:
  • Not using cookie-parser so req.cookies is undefined
  • Trying to access cookies as a string
  • Assuming cookie names are case-insensitive
4. What is wrong with this Express code snippet if req.cookies is always undefined?
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();

app.get('/', (req, res) => {
  res.send(req.cookies);
});

app.use(cookieParser());
medium
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

  1. Step 1: Check middleware order

    Middleware must be added before routes to affect req objects in those routes.
  2. 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.
  3. Final Answer:

    cookie-parser middleware is added after the route, so req.cookies is undefined -> Option A
  4. 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

  1. Step 1: Check cookie access and comparison

    Use strict equality === to compare req.cookies.theme to the string 'dark'.
  2. 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.
  3. Final Answer:

    The code using strict equality req.cookies.theme === 'dark' -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard