0
0
Expressframework~10 mins

req.cookies with cookie-parser in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the cookie-parser middleware.

Express
const express = require('express');
const cookieParser = require([1]);
Drag options to blanks, or click blank then click option'
A'cors'
B'express'
C'body-parser'
D'cookie-parser'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of 'cookie-parser' in require.
Confusing cookie-parser with body-parser.
2fill in blank
medium

Complete the code to use cookie-parser middleware in the Express app.

Express
const app = express();
app.use([1]());
Drag options to blanks, or click blank then click option'
Aexpress.json
BcookieParser
Cexpress.urlencoded
Dcors
Attempts:
3 left
💡 Hint
Common Mistakes
Using express.json() instead of cookieParser().
Forgetting to call the function with parentheses.
3fill in blank
hard

Fix the error in accessing cookies from the request object.

Express
app.get('/', (req, res) => {
  const userCookie = req.[1].user;
  res.send(`User cookie: ${userCookie}`);
});
Drag options to blanks, or click blank then click option'
Acookies
Bcookie
Cheaders
Dparams
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.cookie instead of req.cookies.
Trying to access cookies from req.headers directly.
4fill in blank
hard

Fill both blanks to set up cookie-parser with a secret and access signed cookies.

Express
const cookieParser = require('cookie-parser');
app.use(cookieParser([1]));

app.get('/signed', (req, res) => {
  const signedCookie = req.[2].token;
  res.send(`Signed cookie: ${signedCookie}`);
});
Drag options to blanks, or click blank then click option'
A'mySecret'
Bcookies
CsignedCookies
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Passing null or no secret when signed cookies are needed.
Accessing signed cookies from req.cookies instead of req.signedCookies.
5fill in blank
hard

Fill all three blanks to create a route that reads a cookie and sets a new cookie.

Express
app.get('/cookie', (req, res) => {
  const oldValue = req.[1].sessionId;
  res.cookie([2], 'abc123', { httpOnly: true });
  res.send(`Old session: ${oldValue}`);
});
Drag options to blanks, or click blank then click option'
Acookies
B'sessionId'
DsignedCookies
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.signedCookies when the cookie is not signed.
Forgetting to put cookie name in quotes when setting cookie.