Bird
Raised Fist0
Expressframework~10 mins

res.status for status codes in Express - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - res.status for status codes
Request received
↓
Set status code with res.status(code)
↓
Attach response data (optional)
↓
Send response with res.send() or res.json()
↓
Client receives response with status code
This flow shows how Express sets HTTP status codes using res.status, then sends the response to the client.
Execution Sample
Express
app.get('/example', (req, res) => {
  res.status(404).send('Not Found');
});
This code sets the HTTP status to 404 and sends a 'Not Found' message to the client.
Execution Table
StepActionres.status CodeResponse BodyEffect
1Request to /example received200undefinedServer prepares to respond
2Call res.status(404)404undefinedStatus code set to 404
3Call res.send('Not Found')404'Not Found'Response sent with status 404 and body
4Client receives response404'Not Found'Client sees 404 status and message
💡 Response sent; Express ends request-response cycle
Variable Tracker
VariableStartAfter Step 2After Step 3Final
res.statusCode200404404404
res.bodyundefinedundefined'Not Found''Not Found'
Key Moments - 2 Insights
Why do we chain res.status(404).send('Not Found') instead of calling them separately?
Because res.status(code) sets the status and returns the res object, allowing chaining. The execution_table row 2 shows status set, and row 3 shows send called on the same res.
What happens if we call res.send() without res.status()?
Express defaults status to 200 OK. The execution_table shows status is 200 before res.status, meaning default 200 is used if not set.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the status code after step 2?
Aundefined
B404
C200
D500
💡 Hint
Check the 'res.status Code' column at step 2 in the execution_table
At which step is the response body set to 'Not Found'?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Response Body' column in the execution_table
If we remove res.status(404), what status code will the client receive?
A500
B404
C200
D302
💡 Hint
Refer to key_moments answer about default status code when res.status is not called
Concept Snapshot
res.status(code) sets HTTP status code for the response
It returns res to allow chaining like res.status(404).send('msg')
If not set, status defaults to 200 OK
Use res.send() or res.json() to send response body
Always set status before sending response
Full Transcript
In Express, when a request comes in, you can set the HTTP status code using res.status(code). This sets the status code for the response. Then you send the response body using res.send() or res.json(). The status code defaults to 200 if you don't set it. The res.status method returns the res object so you can chain calls like res.status(404).send('Not Found'). This flow ensures the client receives the correct status and message.

Practice

(1/5)
1. What does res.status(404) do in an Express route handler?
easy
A. Changes the request method to GET
B. Sends a 404 error message to the client automatically
C. Sets the HTTP response status code to 404 (Not Found)
D. Stops the server from running

Solution

  1. Step 1: Understand the purpose of res.status

    res.status(code) sets the HTTP status code for the response but does not send the response itself.
  2. Step 2: Identify what 404 means

    404 is the standard HTTP status code meaning "Not Found".
  3. Final Answer:

    Sets the HTTP response status code to 404 (Not Found) -> Option C
  4. Quick Check:

    res.status(404) = sets status code 404 [OK]
Hint: res.status sets code; use res.send or res.json to send [OK]
Common Mistakes:
  • Thinking res.status sends the response automatically
  • Confusing status code with request method
  • Assuming res.status stops server execution
2. Which of the following is the correct way to send a 201 status with a JSON response in Express?
easy
A. res.status.json(201, { message: 'Created' })
B. res.send(201).json({ message: 'Created' })
C. res.json(201, { message: 'Created' })
D. res.status(201).json({ message: 'Created' })

Solution

  1. Step 1: Recall correct chaining syntax

    In Express, res.status(code) sets the status code, then res.json(data) sends the JSON response.
  2. Step 2: Check each option's syntax

    res.status(201).json({ message: 'Created' }) correctly chains res.status(201) with res.json({ message: 'Created' }). Others misuse method order or parameters.
  3. Final Answer:

    res.status(201).json({ message: 'Created' }) -> Option D
  4. Quick Check:

    Correct chaining = res.status().json() [OK]
Hint: Use res.status(code).json(data) to set code and send JSON [OK]
Common Mistakes:
  • Calling res.send before res.status
  • Passing status code as first argument to res.json
  • Using res.status.json which is invalid
3. What will the client receive when this Express handler runs?
app.get('/test', (req, res) => {
  res.status(500);
  res.send('Server error');
});
medium
A. A response with status 200 and body 'Server error'
B. A response with status 500 and body 'Server error'
C. A response with status 500 and empty body
D. An error thrown by Express

Solution

  1. Step 1: Analyze res.status and res.send usage

    res.status(500) sets the HTTP status code to 500. Then res.send('Server error') sends the response body with that status.
  2. Step 2: Determine final response

    The client receives status 500 with the text 'Server error' as the body.
  3. Final Answer:

    A response with status 500 and body 'Server error' -> Option B
  4. Quick Check:

    res.status(500) + res.send('Server error') = 500 + body [OK]
Hint: res.status sets code; res.send sends body with that code [OK]
Common Mistakes:
  • Assuming res.send resets status to 200
  • Thinking status 500 causes Express error automatically
  • Believing response body is empty without res.send argument
4. Identify the error in this Express code snippet:
app.get('/error', (req, res) => {
  res.send('Oops!');
  res.status(400);
});
medium
A. res.status(400) is called after res.send, so status code is ignored
B. res.send cannot be called without res.status first
C. res.status must be called with a string, not a number
D. No error, this code works correctly

Solution

  1. Step 1: Understand response flow in Express

    Once res.send() is called, the response is sent and closed. Any code after it won't affect the response.
  2. Step 2: Analyze order of calls

    Here, res.send('Oops!') sends the response with default status 200. Then res.status(400) is called but ignored because response is already sent.
  3. Final Answer:

    res.status(400) is called after res.send, so status code is ignored -> Option A
  4. Quick Check:

    Call res.status before res.send to set status [OK]
Hint: Always call res.status before res.send or res.json [OK]
Common Mistakes:
  • Calling res.status after res.send expecting status change
  • Thinking res.send requires res.status first
  • Using string instead of number in res.status
5. You want to send a 204 No Content status with no response body in Express. Which code snippet correctly does this?
hard
A. res.status(204).send()
B. All of the above
C. res.status(204).json(null)
D. res.sendStatus(204)

Solution

  1. Step 1: Understand 204 No Content meaning

    Status 204 means the server successfully processed the request but returns no content in the response body (Content-Length: 0).
  2. Step 2: Check each option's behavior

    res.status(204).send() sends status 204 with empty body [check].
    res.status(204).json(null) sends status 204 with JSON "null" body ✗.
    res.sendStatus(204) sends status 204 with "No Content" text body ✗.
    Only A sends no response body.
  3. Final Answer:

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

    res.status(204).send() = 204 + empty body [OK]
Hint: Use res.status(204).send() for 204 with no body [OK]
Common Mistakes:
  • Sending body content with 204 status (should be empty)
  • Using res.status(204).json({}) which sends empty object body
  • Not sending response after setting status
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard