Complete the code to import the cookie-parser middleware.
const express = require('express'); const cookieParser = require([1]);
The cookie-parser package is imported by requiring 'cookie-parser'.
Complete the code to use cookie-parser middleware in the Express app.
const app = express();
app.use([1]());To enable cookie parsing, you use cookieParser() as middleware.
Fix the error in accessing cookies from the request object.
app.get('/', (req, res) => { const userCookie = req.[1].user; res.send(`User cookie: ${userCookie}`); });
Cookies parsed by cookie-parser are available on req.cookies.
Fill both blanks to set up cookie-parser with a secret and access signed cookies.
const cookieParser = require('cookie-parser'); app.use(cookieParser([1])); app.get('/signed', (req, res) => { const signedCookie = req.[2].token; res.send(`Signed cookie: ${signedCookie}`); });
Passing a secret string to cookie-parser enables signed cookies, which are accessed via req.signedCookies.
Fill all three blanks to create a route that reads a cookie and sets a new cookie.
app.get('/cookie', (req, res) => { const oldValue = req.[1].sessionId; res.cookie([2], 'abc123', { httpOnly: true }); res.send(`Old session: ${oldValue}`); });
Cookies are read from req.cookies. To set a cookie, use res.cookie('cookieName', value). Here, the cookie name is 'sessionId'.