0
0
Expressframework~10 mins

Status code conventions in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to send a 200 OK status response in Express.

Express
app.get('/home', (req, res) => {
  res.status([1]).send('Welcome!');
});
Drag options to blanks, or click blank then click option'
A200
B404
C500
D301
Attempts:
3 left
💡 Hint
Common Mistakes
Using 404 or 500 which mean errors, not success.
2fill in blank
medium

Complete the code to send a 404 Not Found status when a resource is missing.

Express
app.get('/item/:id', (req, res) => {
  const item = findItem(req.params.id);
  if (!item) {
    res.status([1]).send('Item not found');
  }
});
Drag options to blanks, or click blank then click option'
A500
B403
C200
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 which means success, or 500 which means server error.
3fill in blank
hard

Fix the error in the code to send a 201 Created status after a new user is added.

Express
app.post('/users', (req, res) => {
  addUser(req.body);
  res.status([1]).send('User created');
});
Drag options to blanks, or click blank then click option'
A400
B201
C200
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 which means OK but not specifically creation.
4fill in blank
hard

Fill both blanks to send a 400 Bad Request status with a JSON error message.

Express
app.post('/login', (req, res) => {
  if (!req.body.username) {
    res.status([1]).json({ error: '[2]' });
  }
});
Drag options to blanks, or click blank then click option'
A400
B401
CBad Request
DUnauthorized
Attempts:
3 left
💡 Hint
Common Mistakes
Using 401 which means unauthorized, not bad request.
5fill in blank
hard

Fill all three blanks to send a 500 Internal Server Error with a JSON message and end the response.

Express
app.get('/data', (req, res) => {
  try {
    const data = getData();
    res.json(data);
  } catch (error) {
    res.status([1]).json({ message: '[2]' }).[3]();
  }
});
Drag options to blanks, or click blank then click option'
A400
BInternal Server Error
Cend
D500
Attempts:
3 left
💡 Hint
Common Mistakes
Using 400 which means client error, not server error.