Challenge - 5 Problems
Status Code Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
HTTP Status Code for Successful Resource Creation
What status code will this Node.js Express handler send when a new user is successfully created?
Node.js
app.post('/users', (req, res) => { // Assume user creation logic here res.status(201).send({ message: 'User created' }); });
Attempts:
2 left
💡 Hint
201 is the standard status code for resource creation success.
✗ Incorrect
The handler uses res.status(201), which means the server successfully created a new resource. 201 is the standard HTTP status code for this.
❓ Predict Output
intermediate2:00remaining
Status Code for Unauthorized Access
What status code will this Express middleware send if the user is not authenticated?
Node.js
function authMiddleware(req, res, next) {
if (!req.user) {
return res.status(401).send('Unauthorized');
}
next();
}Attempts:
2 left
💡 Hint
401 means the user must authenticate first.
✗ Incorrect
The middleware sends status 401 when req.user is missing, indicating the user is not authenticated.
❓ component_behavior
advanced2:00remaining
Correct Status Code for Invalid Input
Which status code should be sent when the client sends invalid data in a POST request?
Node.js
app.post('/submit', (req, res) => { if (!req.body.name) { res.status(400).send('Name is required'); } else { res.status(200).send('Success'); } });
Attempts:
2 left
💡 Hint
400 means the client sent bad data.
✗ Incorrect
400 Bad Request is the correct status code when the client sends invalid or missing data.
❓ Predict Output
advanced2:00remaining
Status Code for Server Error
What status code will this Express error handler send when an unexpected error occurs?
Node.js
app.use((err, req, res, next) => {
console.error(err);
res.status(500).send('Internal Server Error');
});Attempts:
2 left
💡 Hint
500 means the server failed unexpectedly.
✗ Incorrect
The error handler sends status 500 to indicate an internal server error.
❓ component_behavior
expert2:00remaining
Correct Status Code for Not Found
Which option correctly sends the status code for a 'Not Found' resource?
Node.js
app.get('/item/:id', (req, res) => { const item = database.find(req.params.id); if (!item) { res.status(404).send('Item not found'); } else { res.status(200).json(item); } });
Attempts:
2 left
💡 Hint
404 means resource not found.
✗ Incorrect
Only option D correctly uses 404 for a missing resource. Other options send wrong status codes.