0
0
Expressframework~10 mins

Why understanding req matters in Express - Test Your Understanding

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

Complete the code to access the URL path from the request object in Express.

Express
app.get('/', (req, res) => { const path = req.[1]; res.send(path); });
Drag options to blanks, or click blank then click option'
Aurl
Bpath
Cbody
Dquery
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.url instead of req.path
Trying to access req.body for URL info
2fill in blank
medium

Complete the code to get a query parameter named 'id' from the request in Express.

Express
app.get('/item', (req, res) => { const id = req.[1].id; res.send(`ID is ${id}`); });
Drag options to blanks, or click blank then click option'
Abody
Bparams
Cquery
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.body for query params
Using req.params which is for route params
3fill in blank
hard

Fix the error in accessing a route parameter named 'userId' in Express.

Express
app.get('/user/:userId', (req, res) => { const user = req.[1].userId; res.send(`User ID: ${user}`); });
Drag options to blanks, or click blank then click option'
Aparams
Bquery
Cbody
Dcookies
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.query or req.body for route params
Trying to access req.cookies.userId
4fill in blank
hard

Fill both blanks to correctly read JSON data sent in a POST request body in Express.

Express
app.post('/data', (req, res) => { const data = req.[1]; const name = data.[2]; res.send(`Name is ${name}`); });
Drag options to blanks, or click blank then click option'
Abody
Bparams
Cname
Dquery
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.query or req.params for POST body data
Trying to access req.body without middleware
5fill in blank
hard

Fill all three blanks to create a route that reads a route param, a query param, and a header value in Express.

Express
app.get('/product/:id', (req, res) => { const productId = req.[1].id; const ref = req.[2].ref; const agent = req.[3]['user-agent']; res.send(`Product: ${productId}, Ref: ${ref}, Agent: ${agent}`); });
Drag options to blanks, or click blank then click option'
Aparams
Bquery
Cheaders
Dbody
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up params and query
Trying to get headers from body or query