0
0
Expressframework~5 mins

req.cookies with cookie-parser in Express - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the cookie-parser middleware in Express?

cookie-parser helps Express read cookies sent by the browser. It parses cookie headers and makes cookies available as req.cookies.

Click to reveal answer
beginner
How do you access cookies in an Express route handler after using cookie-parser?

You access cookies using req.cookies. For example, req.cookies.username gives the value of the cookie named 'username'.

Click to reveal answer
beginner
Show the code to set up cookie-parser in an Express app.
<pre>import express from 'express';
import cookieParser from 'cookie-parser';

const app = express();
app.use(cookieParser());</pre>
Click to reveal answer
intermediate
What happens if you try to read req.cookies without using cookie-parser?

req.cookies will be undefined because Express does not parse cookies by default. You must use cookie-parser to populate it.

Click to reveal answer
intermediate
Can cookie-parser parse signed cookies? How do you enable this?

Yes, cookie-parser can parse signed cookies if you provide a secret string when you initialize it, like app.use(cookieParser('secret')). Signed cookies appear in req.signedCookies.

Click to reveal answer
What does req.cookies contain in an Express app using cookie-parser?
AThe session data stored on the server
BThe raw cookie header string
CAn object with cookie names and values sent by the client
DThe response cookies to be sent
How do you add cookie-parser middleware to an Express app?
Aapp.use(cookieParser())
Bapp.cookieParser()
Capp.set('cookie-parser')
Dapp.use(express.cookieParser())
If you want to read signed cookies, where do you find them?
Areq.cookies
Breq.signed
Creq.headers.cookie
Dreq.signedCookies
What will happen if you forget to use cookie-parser but try to access req.cookies?
A<code>req.cookies</code> will be undefined
B<code>req.cookies</code> will be an empty object
CExpress will throw an error
DCookies will be parsed automatically
Which of these is NOT a feature of cookie-parser?
AParsing cookies from request headers
BSigning cookies automatically on response
CProviding <code>req.cookies</code> and <code>req.signedCookies</code>
DSupporting cookie signature verification
Explain how to use cookie-parser in an Express app to read cookies sent by the browser.
Think about middleware setup and how Express handles requests.
You got /3 concepts.
    Describe the difference between req.cookies and req.signedCookies when using cookie-parser.
    Consider how cookie security is handled.
    You got /3 concepts.