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 usage patterns in Node.js
Introduction
Syntax
Node.js
res.status(code).send(body)
code is a number like 200, 404, or 500.
res is the response object in Node.js frameworks like Express.
Examples
Node.js
res.status(200).send('OK')
Node.js
res.status(404).send('Not Found')
Node.js
res.status(500).send('Server Error')
Sample Program
This simple Express server shows three routes. Each route sends a different status code and message to explain what happened.
Node.js
import express from 'express'; const app = express(); app.get('/success', (req, res) => { res.status(200).send('Request succeeded'); }); app.get('/notfound', (req, res) => { res.status(404).send('Page not found'); }); app.get('/error', (req, res) => { res.status(500).send('Internal server error'); }); 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), 401 (Unauthorized), 404 (Not Found), 500 (Server Error).
Use res.status() before sending the response body.
Summary
Status codes tell the client what happened with the request.
Use res.status(code) to set the status code in Node.js.
Pick the right code to make your app clear and easy to debug.
Practice
1. What does the HTTP status code
200 generally indicate in a Node.js response?easy
Solution
Step 1: Understand HTTP status codes
HTTP status codes communicate the result of a client's request to the server.Step 2: Identify the meaning of 200
Status code 200 means the request succeeded and the server sent back the expected data.Final Answer:
The request was successful and the server returned the requested data. -> Option BQuick Check:
200 = Success [OK]
Hint: 200 means success, data sent back properly [OK]
Common Mistakes:
- Confusing 200 with error codes like 400 or 500
- Thinking 200 means resource not found
- Mixing up client and server error codes
2. Which of the following is the correct way to set a 404 status code in a Node.js Express response?
easy
Solution
Step 1: Recall Express response methods
Express usesres.status(code)to set status codes before sending a response.Step 2: Check method correctness
res.status(404).send('Not Found');correctly sets status 404 and sends the message.Final Answer:
res.status(404).send('Not Found'); -> Option AQuick Check:
Use res.status(code) to set status [OK]
Hint: Use res.status(code) before sending response [OK]
Common Mistakes:
- Using non-existent methods like res.code or res.setStatus
- Passing status code as second argument to send
- Confusing sendStatus with status
3. Consider this Express code snippet:
What status code will the server respond with if the client calls
app.get('/data', (req, res) => {
if (!req.query.id) {
return res.status(400).send('Missing id');
}
res.status(200).send('Data for ' + req.query.id);
});What status code will the server respond with if the client calls
/data without an id query parameter?medium
Solution
Step 1: Analyze the condition for missing id
The code checks ifreq.query.idis missing and returns status 400 with message 'Missing id'.Step 2: Determine response when id is missing
Ifidis absent, the server responds immediately with status 400 (bad request).Final Answer:
400 -> Option AQuick Check:
Missing required data = 400 [OK]
Hint: Missing required input usually means 400 Bad Request [OK]
Common Mistakes:
- Assuming 404 means missing query parameter
- Thinking 200 is always returned
- Confusing 500 server error with client error
4. You have this Express route:
What is the problem with this code?
app.post('/submit', (req, res) => {
if (!req.body.name) {
res.sendStatus(422);
}
res.status(201).send('Created');
});What is the problem with this code?
medium
Solution
Step 1: Check response flow when name is missing
If!req.body.nameis true,res.sendStatus(422)sends a response but code continues.Step 2: Identify double response issue
After sending 422, the code still callsres.status(201).send('Created'), causing an error because response was already sent.Final Answer:
It sends two responses ifnameis missing, causing an error. -> Option DQuick Check:
Send response once per request [OK]
Hint: Return after sending response to avoid double sends [OK]
Common Mistakes:
- Not returning after sending a response
- Confusing 422 with invalid status code
- Thinking order of status calls matters without return
5. You want to create an Express route that returns a JSON error with status 403 if the user is not authorized, and status 200 with data if authorized. Which code snippet correctly implements this pattern?
hard
Solution
Step 1: Check response flow for unauthorized user
app.get('/secret', (req, res) => { if (!req.user) { return res.status(403).json({ error: 'Forbidden' }); } res.status(200).json({ data: 'Secret info' }); }); usesreturnafter sending 403 JSON error, preventing further code execution.Step 2: Confirm authorized user response
It sends status 200 with JSON data correctly if user exists.Final Answer:
app.get('/secret', (req, res) => { if (!req.user) { return res.status(403).json({ error: 'Forbidden' }); } res.status(200).json({ data: 'Secret info' }); }); -> Option CQuick Check:
Return after error response to avoid double sends [OK]
Hint: Use return after error response to stop execution [OK]
Common Mistakes:
- Not returning after sending error response
- Using sendStatus with json chaining incorrectly
- Sending plain text instead of JSON for API
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
