0
0
Node.jsframework~10 mins

Status code usage patterns in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Status code usage patterns
Receive HTTP Request
Process Request Logic
Determine Status Code
Set Response Status Code
Send Response with Status Code
Client Receives Response
Client Handles Based on Status Code
This flow shows how a Node.js server receives a request, decides the right status code, sends it back, and how the client uses it.
Execution Sample
Node.js
import http from 'http';

const server = http.createServer((req, res) => {
  if (req.url === '/success') {
    res.statusCode = 200;
    res.end('OK');
  } else {
    res.statusCode = 404;
    res.end('Not Found');
  }
});

server.listen(3000);
A simple Node.js server that sends 200 for '/success' and 404 for other URLs.
Execution Table
StepRequest URLConditionStatus Code SetResponse Sent
1/successreq.url === '/success' is true200'OK'
2/unknownreq.url === '/success' is false404'Not Found'
3-No more requests--
💡 No more requests to process, server keeps running.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
req.urlundefined/success/unknownvaries per request
res.statusCode200 (default)200404varies per request
res.end()not calledcalled with 'OK'called with 'Not Found'called per request
Key Moments - 3 Insights
Why do we set res.statusCode before calling res.end()?
Because the status code must be set before sending the response body; see execution_table rows 1 and 2 where statusCode is set before res.end() is called.
What happens if we don't set a status code explicitly?
Node.js defaults to 200 OK if no status code is set; this is why res.statusCode starts at 200 in variable_tracker.
Why does the server send 404 for unknown URLs?
Because the condition req.url === '/success' is false, so the else branch sets statusCode to 404 and sends 'Not Found' as shown in execution_table row 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what status code is sent when the request URL is '/success'?
A404
B500
C200
D301
💡 Hint
Check execution_table row 1 under 'Status Code Set'
At which step does the server send a 'Not Found' response?
AStep 1
BStep 2
CStep 3
DNo step sends 'Not Found'
💡 Hint
Look at execution_table row 2 under 'Response Sent'
If we add a new URL '/error' that sets statusCode to 500, how would the execution_table change?
AAdd a new row with statusCode 500 and appropriate response
BChange step 1 statusCode to 500
CRemove step 2
DNo change needed
💡 Hint
New request URLs add new rows as seen in execution_table rows 1 and 2
Concept Snapshot
Status code usage in Node.js:
- Set res.statusCode before res.end()
- 200 means success
- 404 means resource not found
- Use status codes to tell client what happened
- Always send a response to end request
- Status codes guide client behavior
Full Transcript
This visual execution shows how a Node.js server handles HTTP requests by checking the URL, setting the right status code, and sending a response. When the URL is '/success', the server sets status code 200 and sends 'OK'. For other URLs, it sets 404 and sends 'Not Found'. The status code must be set before sending the response body. If no status code is set, Node.js defaults to 200. This pattern helps clients understand if their request succeeded or failed. The execution table tracks each step, showing the request URL, condition checked, status code set, and response sent. Variable tracker shows how request URL and response status code change per request. Key moments clarify why order matters and why 404 is sent for unknown URLs. The quiz tests understanding of status codes sent at each step and how adding new URLs affects the flow.