Complete the code to access the URL path from the request object in Express.
app.get('/', (req, res) => { const path = req.[1]; res.send(path); });
The req.path property gives the URL path of the request, which is useful to know what route was accessed.
Complete the code to get a query parameter named 'id' from the request in Express.
app.get('/item', (req, res) => { const id = req.[1].id; res.send(`ID is ${id}`); });
The req.query object holds query string parameters from the URL, like '?id=123'.
Fix the error in accessing a route parameter named 'userId' in Express.
app.get('/user/:userId', (req, res) => { const user = req.[1].userId; res.send(`User ID: ${user}`); });
Route parameters like ':userId' are accessed via req.params.
Fill both blanks to correctly read JSON data sent in a POST request body in Express.
app.post('/data', (req, res) => { const data = req.[1]; const name = data.[2]; res.send(`Name is ${name}`); });
JSON data sent in POST requests is accessed via req.body. Then you get the property like name.
Fill all three blanks to create a route that reads a route param, a query param, and a header value in 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}`); });
Route params are in req.params, query params in req.query, and headers in req.headers.