Complete the code to define a REST API endpoint that returns a list of resources.
app.get('/resources', (req, res) => { res.[1](resources); });
The send method sends the response back to the client with the resources data.
Complete the code to extract the resource ID from the URL parameters.
app.get('/resources/:id', (req, res) => { const resourceId = req.[1].id; res.send(findResourceById(resourceId)); });
The params object contains URL parameters like :id.
Fix the error in the code to correctly create a new resource from the request body.
app.post('/resources', (req, res) => { const newResource = req.[1]; resources.push(newResource); res.status(201).send(newResource); });
The body property contains the data sent by the client in POST requests.
Fill both blanks to update a resource by ID and send the updated resource.
app.put('/resources/:id', (req, res) => { const id = req.[1].id; const updatedData = req.[2]; const index = resources.findIndex(r => r.id === id); if (index !== -1) { resources[index] = {...resources[index], ...updatedData}; res.send(resources[index]); } else { res.status(404).send({error: 'Resource not found'}); } });
URL parameters are accessed via req.params and the update data is in req.body.
Fill all three blanks to delete a resource by ID and send a confirmation message.
app.delete('/resources/:id', (req, res) => { const id = req.[1].id; const index = resources.findIndex(r => r.id === id); if (index !== -1) { resources.[2](index, 1); res.[3]({ message: 'Resource deleted' }); } else { res.status(404).send({ error: 'Resource not found' }); } });
The ID is from req.params, splice removes the resource from the array, and send sends the confirmation message.