Bird
Raised Fist0
Node.jsframework~10 mins

Response methods and status codes in Node.js - 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 - Response methods and status codes
Receive HTTP Request
Choose Response Method
Set Status Code
Send Response Body
Client Receives Response
The server receives a request, selects a response method, sets a status code, sends the response, and the client receives it.
Execution Sample
Node.js
res.status(404).send('Not Found');
Sets the HTTP status to 404 and sends 'Not Found' as the response body.
Execution Table
StepActionStatus CodeResponse BodyResult
1Call res.status(404)404undefinedStatus code set to 404
2Call res.send('Not Found')404'Not Found'Response sent with status 404 and body
3Client receives response404'Not Found'Client sees 404 status and message
4End404'Not Found'Response cycle complete
💡 Response sent, cycle ends
Variable Tracker
VariableStartAfter Step 1After Step 2Final
statusCode200 (default)404404404
responseBodyundefinedundefined'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 sets the status code and returns the response object, allowing send to immediately send the response with that status. See execution_table steps 1 and 2.
What happens if we call res.send() without setting a status code first?
The response uses the default status code 200 (OK). This is shown in variable_tracker where statusCode starts at 200.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the status code after step 1?
A500
B404
C200
DUndefined
💡 Hint
Check the 'Status Code' column at step 1 in execution_table
At which step is the response body set to 'Not Found'?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Response Body' column in execution_table
If we omit res.status(404) and only call res.send('OK'), what status code will the client receive?
A200
B404
C500
DUndefined
💡 Hint
Refer to variable_tracker where statusCode starts at 200 by default
Concept Snapshot
res.status(code) sets HTTP status code
res.send(body) sends response body
Chain methods: res.status(404).send('Not Found')
Default status is 200 if not set
Client receives status and body together
Full Transcript
In Node.js, when handling HTTP requests, you use response methods to send data back to the client. First, you can set the status code with res.status(code). This changes the HTTP status like 404 for Not Found or 200 for OK. Then, you send the response body with res.send(body). These methods can be chained so that res.status(404).send('Not Found') sets the status and sends the message in one go. If you don't set a status, it defaults to 200. The client receives both the status code and the response body together. This flow ensures clear communication about the result of the request.

Practice

(1/5)
1. What does the res.status(404) method do in a Node.js response?
easy
A. Redirects the client to a page named '404'
B. Sends the response body with the text '404'
C. Sets the HTTP status code to 404 (Not Found) for the response
D. Ends the response without sending any data

Solution

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

    This method sets the HTTP status code that tells the browser what happened with the request.
  2. Step 2: Identify the meaning of 404 status code

    Status code 404 means 'Not Found', indicating the requested resource does not exist.
  3. Final Answer:

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

    Status code 404 = Not Found [OK]
Hint: Status codes tell browser what happened; 404 means not found [OK]
Common Mistakes:
  • Thinking res.status sends the response body
  • Confusing status code with redirect
  • Assuming res.status ends the response
2. Which of the following is the correct way to send a JSON response with status 200 in Express?
easy
A. res.status(200).json({ message: 'OK' })
B. res.send(200).json({ message: 'OK' })
C. res.json(200, { message: 'OK' })
D. res.status(200).sendJson({ message: 'OK' })

Solution

  1. Step 1: Recall Express response chaining

    Use res.status(code) to set status, then call json() to send JSON data.
  2. Step 2: Check each option's syntax

    res.status(200).json({ message: 'OK' }) correctly chains status(200) and json(). Others misuse method order or names.
  3. Final Answer:

    res.status(200).json({ message: 'OK' }) -> Option A
  4. Quick Check:

    Set status then send JSON = res.status(200).json({ message: 'OK' }) [OK]
Hint: Set status first, then call json() to send data [OK]
Common Mistakes:
  • Calling send before status
  • Using non-existent sendJson method
  • Passing status code inside json()
3. What will the following Express code send to the client?
app.get('/test', (req, res) => {
  res.status(201).send('Created');
});
medium
A. A response with status 200 and body 'Created'
B. A response with status 201 and body 'Created'
C. A response with status 201 and no body
D. An error because send() cannot follow status()

Solution

  1. Step 1: Understand status and send chaining

    res.status(201) sets status code 201 (Created), then send('Created') sends that text as the response body.
  2. Step 2: Confirm behavior of Express response

    Express allows chaining status and send; the response will have status 201 and body 'Created'.
  3. Final Answer:

    A response with status 201 and body 'Created' -> Option B
  4. Quick Check:

    Status 201 + send text = A response with status 201 and body 'Created' [OK]
Hint: status sets code, send sends body; chaining works [OK]
Common Mistakes:
  • Assuming default status 200 is sent
  • Thinking send() clears status
  • Believing chaining status and send causes error
4. Identify the error in this Express code snippet:
app.get('/redirect', (req, res) => {
  res.send('Redirecting...');
  res.redirect('/new-url');
});
medium
A. Calling res.redirect after res.send causes an error
B. res.redirect should be called before res.send
C. res.send cannot send strings, only JSON
D. No error; this code works fine

Solution

  1. Step 1: Understand response flow in Express

    Once res.send() is called, the response is sent and closed.
  2. Step 2: Check order of methods

    Calling res.redirect() after res.send() tries to send headers again, causing an error.
  3. Final Answer:

    Calling res.redirect after res.send causes an error -> Option A
  4. Quick Check:

    Send ends response; redirect after send fails [OK]
Hint: Send or redirect ends response; don't call both [OK]
Common Mistakes:
  • Thinking order doesn't matter
  • Believing send only works with JSON
  • Assuming no error on multiple sends
5. You want to send a JSON response with status 400 and a message 'Invalid input'. Which code correctly does this in Express?
hard
A. res.json(400, { error: 'Invalid input' })
B. res.sendStatus(400).json({ error: 'Invalid input' })
C. res.status(400).send('Invalid input')
D. res.status(400).json({ error: 'Invalid input' })

Solution

  1. Step 1: Set the status code correctly

    Use res.status(400) to set the HTTP status to 400 (Bad Request).
  2. Step 2: Send JSON response

    Use json() to send the JSON object with the error message.
  3. Step 3: Check incorrect options

    res.json(400, { error: 'Invalid input' }) uses wrong method signature (sends 400 as JSON body with status 200); res.status(400).send('Invalid input') sends plain text string (Content-Type text/html) instead of JSON; res.sendStatus(400).json({ error: 'Invalid input' }) ends response with sendStatus before json.
  4. Final Answer:

    res.status(400).json({ error: 'Invalid input' }) -> Option D
  5. Quick Check:

    Status then json() sends JSON with code [OK]
Hint: Use status() then json() to send JSON with status [OK]
Common Mistakes:
  • Passing status code inside json()
  • Using send instead of json for JSON data
  • Calling sendStatus before json