Quiz Entry - updated: 2026.06.20
How do you handle a resource not found in a REST API?
Respond with HTTP status 404 and an error message body when the requested resource does not exist.
app.get('/users/:id', (req, res) => {
const user = users.find(u => u.id === req.params.id);
if (!user) {
return res.status(404).json({
error: 'User not found'
});
}
res.json(user);
});
Use appropriate status codes: 400 (bad request), 401 (unauthorized), 404 (not found), 500 (server error).