Bird
Raised Fist0
Expressframework~10 mins

Status code conventions in Express - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Status code conventions
Client sends HTTP request
Server processes request
Determine response status code
Send response with status code
Client receives response and acts accordingly
This flow shows how a server using Express decides and sends HTTP status codes back to the client after processing a request.
Execution Sample
Express
app.get('/user', (req, res) => {
  if (!req.query.id) {
    res.status(400).send('Bad Request');
  } else {
    res.status(200).send('User found');
  }
});
This Express route sends a 400 status if no user ID is given, otherwise sends 200.
Execution Table
StepCondition CheckedStatus Code SetResponse SentExplanation
1Is req.query.id present?NoNoCheck if user ID is missing
2req.query.id missing400'Bad Request'Send 400 Bad Request because ID is missing
3Is req.query.id present?YesNoUser ID is present, proceed
4req.query.id present200'User found'Send 200 OK with user found message
💡 Response sent with appropriate status code based on presence of user ID
Variable Tracker
VariableStartAfter Step 1After Step 3Final
req.query.idundefined or valueundefined or valuevalue if presentvalue if present
res.statusCodeunset400 if missing200 if present400 or 200 depending on condition
Key Moments - 2 Insights
Why do we use different status codes like 400 and 200?
Because 400 means the client sent a bad request (missing ID), while 200 means the request was successful. See execution_table rows 2 and 4.
What happens if we forget to set a status code?
Express defaults to 200 OK if no status is set. So the client might think the request succeeded even if it didn't.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what status code is sent when req.query.id is missing?
A404
B400
C200
D500
💡 Hint
Check row 2 in the execution_table where req.query.id is missing
At which step does the server send a 200 status code?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Look at the execution_table row where status code 200 is set
If we remove res.status(400), what status code will Express send when ID is missing?
A200
B400
C404
D500
💡 Hint
Recall Express defaults to 200 if no status is set (see key_moments answer 2)
Concept Snapshot
Status codes tell the client how the request went.
400 means bad request (client error).
200 means success.
Use res.status(code) before sending response.
Always send correct status for clarity.
Full Transcript
In Express, when a client sends a request, the server checks conditions like if required data is present. Based on that, it sets a status code using res.status(code). For example, if a user ID is missing, the server sends 400 Bad Request. If the ID is present, it sends 200 OK. This helps the client understand if the request succeeded or failed. If no status is set, Express defaults to 200. So always set the right status code before sending a response.

Practice

(1/5)
1. In Express, which status code is conventionally used to indicate a successful GET request?
easy
A. 301 Moved Permanently
B. 404 Not Found
C. 500 Internal Server Error
D. 200 OK

Solution

  1. Step 1: Understand HTTP status codes for success

    The status code 200 means the request was successful and the server returned the requested data.
  2. Step 2: Match the code to the GET request success

    For a successful GET request, 200 OK is the standard code to indicate success.
  3. Final Answer:

    200 OK -> Option D
  4. Quick Check:

    Success code for GET = 200 OK [OK]
Hint: 200 means success, use it for successful GET requests [OK]
Common Mistakes:
  • Using 404 for success
  • Using 500 for client errors
  • Confusing 301 with success
2. Which of the following is the correct way to set a 404 status code in Express?
easy
A. res.statusCode = 404; res.send('Not Found')
B. res.sendStatus(200)
C. res.status(404).send('Not Found')
D. res.code(404).send('Not Found')

Solution

  1. Step 1: Recall Express method to set status code

    Express uses res.status(code) to set the HTTP status code before sending a response.
  2. Step 2: Verify correct syntax for 404

    res.status(404).send('Not Found') correctly sets status 404 and sends the message.
  3. Final Answer:

    res.status(404).send('Not Found') -> Option C
  4. Quick Check:

    Use res.status(code) to set status [OK]
Hint: Use res.status(code) before send() to set status [OK]
Common Mistakes:
  • Using res.code() which doesn't exist
  • Setting res.statusCode directly without chaining
  • Using sendStatus(200) for 404
3. What status code will the following Express code send to the client?
app.get('/data', (req, res) => {
  res.status(201).send('Created');
});
medium
A. 201 Created
B. 404 Not Found
C. 200 OK
D. 500 Internal Server Error

Solution

  1. Step 1: Identify the status code set in the code

    The code uses res.status(201) which sets the HTTP status code to 201.
  2. Step 2: Understand the meaning of 201

    Status 201 means the request was successful and a new resource was created.
  3. Final Answer:

    201 Created -> Option A
  4. Quick Check:

    res.status(201) sends 201 Created [OK]
Hint: res.status(201) means resource created successfully [OK]
Common Mistakes:
  • Assuming default 200 status
  • Confusing 201 with 404
  • Ignoring the status() call
4. You wrote this Express code but clients always get status 200 instead of 400:
app.post('/submit', (req, res) => {
  if (!req.body.name) {
    res.status(400);
    res.send('Name is required');
  }
});

What is the main problem?
medium
A. res.send() must come before res.status()
B. res.status(400) must be followed by return to stop execution
C. 400 is not a valid status code
D. res.status() does not set the status code

Solution

  1. Step 1: Analyze the code flow after setting status 400

    res.status(400) sets the status but code continues to run after sending response.
  2. Step 2: Understand Express response behavior

    Without return, Express may continue and send default 200 later or cause errors.
  3. Final Answer:

    res.status(400) must be followed by return to stop execution -> Option B
  4. Quick Check:

    Use return after res.status().send() to stop further processing [OK]
Hint: Add return after res.status().send() to prevent default 200 [OK]
Common Mistakes:
  • Not returning after sending response
  • Calling res.send() before res.status()
  • Thinking 400 is invalid
5. You want to send a 204 No Content status after deleting a resource in Express. Which code snippet correctly does this?
hard
A. res.status(204).send()
B. res.sendStatus(204).send('Deleted')
C. res.status(204).send('Deleted')
D. res.send(204)

Solution

  1. Step 1: Understand 204 No Content meaning

    204 means success but no response body should be sent.
  2. Step 2: Choose code that sends 204 without content

    res.status(204).send() sends status 204 with empty body, which is correct.
  3. Step 3: Identify incorrect options

    res.sendStatus(204).send('Deleted') tries to chain send() after sendStatus(), which is invalid. res.status(204).send('Deleted') sends a body with 204, which breaks the rule. res.send(204) sends 204 as body, not status.
  4. Final Answer:

    res.status(204).send() -> Option A
  5. Quick Check:

    204 means no content, so send empty response [OK]
Hint: Use res.status(204).send() to send no content response [OK]
Common Mistakes:
  • Sending body with 204 status
  • Using sendStatus(204).send()
  • Sending status code as body