0
0
Expressframework~20 mins

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

Choose your learning style9 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.