Complete the code to send a JSON error response with status 404.
app.get('/item/:id', (req, res) => { const item = database.find(i => i.id === req.params.id); if (!item) { return res.status([1]).json({ error: 'Item not found' }); } res.json(item); });
Use status code 404 to indicate the requested item was not found.
Complete the code to send a JSON error response with a custom message and status 400.
app.post('/user', (req, res) => { if (!req.body.name) { return res.status([1]).json({ error: 'Name is required' }); } res.json({ success: true }); });
Status 400 means 'Bad Request', suitable when required data is missing.
Fix the error in the code to correctly send a JSON error response with status 500.
app.get('/data', (req, res) => { try { const data = getData(); res.json(data); } catch (error) { res.status([1]).json({ error: error.message }); } });
Use status 500 for server errors and send JSON with res.status(500).json(...).
Fill both blanks to send a JSON error response with status 401 and a message.
app.use((req, res) => {
res.status([1]).json({
error: [2]
});
});Status 401 means unauthorized. The error message should explain the issue.
Fill all three blanks to send a JSON error response with status 403 and a detailed message.
app.get('/admin', (req, res) => { if (!req.user || !req.user.isAdmin) { return res.status([1]).json({ error: [2], message: [3] }); } res.json({ secret: '42' }); });
Status 403 means forbidden. The error and message explain the access denial.