Using res.status to Send HTTP Status Codes in Express
📖 Scenario: You are building a simple Express server that responds to client requests. You want to send proper HTTP status codes to tell the client if the request was successful or if there was an error.
🎯 Goal: Build an Express route that uses res.status to send different HTTP status codes along with messages.
📋 What You'll Learn
Create an Express app with a GET route at /status
Add a variable isSuccess to control the response status
Use res.status to send 200 if isSuccess is true
Use res.status to send 500 if isSuccess is false
Send a JSON message with a message property describing the status
💡 Why This Matters
🌍 Real World
Web servers use HTTP status codes to tell browsers or apps if requests worked or failed. This helps users understand what happened.
💼 Career
Backend developers must send correct status codes to build reliable APIs and web services.
Progress0 / 4 steps
1
Set up Express app and route
Create an Express app by requiring express and calling express(). Then create a GET route at /status with a callback function that takes req and res as parameters.
Express
Hint
Remember to require Express and create an app instance. Use app.get to define the route.
2
Add a variable to control success
Inside the /status route callback, create a variable called isSuccess and set it to true.
Express
Hint
Just create a constant named isSuccess and assign it the value true.
3
Send status 200 or 500 based on isSuccess
Use an if statement to check if isSuccess is true. If yes, use res.status(200) and send a JSON object with { message: 'Success' }. Otherwise, use res.status(500) and send { message: 'Error' }.
Express
Hint
Use res.status(code).json(object) to send status and JSON response.
4
Start the server on port 3000
Add code to start the Express server by calling app.listen on port 3000 with a callback that logs 'Server running on port 3000'.
Express
Hint
Use app.listen(port, callback) to start the server and log a 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
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.
Step 2: Identify what 404 means
404 is the standard HTTP status code meaning "Not Found".
Final Answer:
Sets the HTTP response status code to 404 (Not Found) -> Option C
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
Step 1: Recall correct chaining syntax
In Express, res.status(code) sets the status code, then res.json(data) sends the JSON response.
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.
Final Answer:
res.status(201).json({ message: 'Created' }) -> Option D
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?
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
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.
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.
Final Answer:
res.status(400) is called after res.send, so status code is ignored -> Option A
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
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).
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.
Final Answer:
res.status(204).send() -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.