0
0
Node.jsframework~20 mins

Status code usage patterns in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Status Code Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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' });
});
A200
B400
C201
D500
Attempts:
2 left
💡 Hint
201 is the standard status code for resource creation success.
Predict Output
intermediate
2: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();
}
A403
B401
C404
D200
Attempts:
2 left
💡 Hint
401 means the user must authenticate first.
component_behavior
advanced
2: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');
  }
});
A400
B404
C500
D201
Attempts:
2 left
💡 Hint
400 means the client sent bad data.
Predict Output
advanced
2: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');
});
A500
B404
C400
D200
Attempts:
2 left
💡 Hint
500 means the server failed unexpectedly.
component_behavior
expert
2: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);
  }
});
Ares.status(400).send('Item not found');
Bres.status(200).send('Item not found');
Cres.status(500).send('Item not found');
Dres.status(404).send('Item not found');
Attempts:
2 left
💡 Hint
404 means resource not found.