Status codes tell the browser or client what happened with a request. They help show if things worked or if there was a problem.
Status code conventions in Express
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Express
res.status(code).send(body)
res is the response object in Express.
status(code) sets the HTTP status code for the response.
Examples
Express
res.status(200).send('OK')
Express
res.status(404).send('Not Found')
Express
res.status(500).send('Server Error')
Express
res.status(201).json({ id: 123 })
Sample Program
This Express app shows how to use status codes for different routes: success (200), not found (404), and created (201).
Express
import express from 'express'; const app = express(); app.get('/', (req, res) => { res.status(200).send('Welcome to the homepage!'); }); app.get('/notfound', (req, res) => { res.status(404).send('Page not found'); }); app.post('/create', (req, res) => { // pretend we create something here res.status(201).json({ message: 'Resource created' }); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Important Notes
Always use the correct status code to help clients understand the response.
Common codes: 200 (OK), 201 (Created), 400 (Bad Request), 404 (Not Found), 500 (Server Error).
You can chain res.status() with other response methods like send() or json().
Summary
Status codes tell clients what happened with their request.
Use res.status(code) in Express to set the code.
Pick codes that match the situation for clear communication.
Practice
1. In Express, which status code is conventionally used to indicate a successful GET request?
easy
Solution
Step 1: Understand HTTP status codes for success
The status code 200 means the request was successful and the server returned the requested data.Step 2: Match the code to the GET request success
For a successful GET request, 200 OK is the standard code to indicate success.Final Answer:
200 OK -> Option DQuick 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
Solution
Step 1: Recall Express method to set status code
Express uses res.status(code) to set the HTTP status code before sending a response.Step 2: Verify correct syntax for 404
res.status(404).send('Not Found') correctly sets status 404 and sends the message.Final Answer:
res.status(404).send('Not Found') -> Option CQuick 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
Solution
Step 1: Identify the status code set in the code
The code uses res.status(201) which sets the HTTP status code to 201.Step 2: Understand the meaning of 201
Status 201 means the request was successful and a new resource was created.Final Answer:
201 Created -> Option AQuick 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:
What is the main problem?
app.post('/submit', (req, res) => {
if (!req.body.name) {
res.status(400);
res.send('Name is required');
}
});What is the main problem?
medium
Solution
Step 1: Analyze the code flow after setting status 400
res.status(400) sets the status but code continues to run after sending response.Step 2: Understand Express response behavior
Without return, Express may continue and send default 200 later or cause errors.Final Answer:
res.status(400) must be followed by return to stop execution -> Option BQuick 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
Solution
Step 1: Understand 204 No Content meaning
204 means success but no response body should be sent.Step 2: Choose code that sends 204 without content
res.status(204).send() sends status 204 with empty body, which is correct.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.Final Answer:
res.status(204).send() -> Option AQuick 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
