0
0
Node.jsframework~10 mins

Routing requests manually in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Routing requests manually
Start Server
Receive HTTP Request
Check Request URL
Handle1
Send Response
End
The server starts and waits for requests. When a request comes, it checks the URL path and sends the matching response or a 404 if no match.
Execution Sample
Node.js
const http = require('http');
const server = http.createServer((req, res) => {
  if (req.url === '/hello') {
    res.end('Hello World');
  } else {
    res.statusCode = 404; res.end('Not Found');
  }
});
server.listen(3000);
This code creates a server that responds with 'Hello World' for '/hello' path and 'Not Found' for others.
Execution Table
StepRequest URLCondition CheckedCondition ResultAction TakenResponse Sent
1/helloreq.url === '/hello'trueSend 'Hello World''Hello World'
2/goodbyereq.url === '/hello'falseSet status 404 and send 'Not Found''Not Found'
3/unknownreq.url === '/hello'falseSet status 404 and send 'Not Found''Not Found'
💡 Requests handled based on URL; unmatched URLs return 404 Not Found.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
req.urlundefined/hello/goodbye/unknown
res.statusCode200 (default)200404404
res contentempty'Hello World''Not Found''Not Found'
Key Moments - 2 Insights
Why does the server send 'Not Found' for URLs other than '/hello'?
Because the condition req.url === '/hello' is false for other URLs, so the else block sets statusCode to 404 and sends 'Not Found' as shown in execution_table rows 2 and 3.
What happens if we forget to call res.end()?
The server will not send the response back properly, causing the client to wait indefinitely. In the code, res.end() finalizes the response, so missing it stops the response from completing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what response is sent when the request URL is '/hello'?
A'Not Found'
B'Hello World'
CEmpty response
DError message
💡 Hint
Check the 'Response Sent' column for Step 1 in the execution_table.
At which step does the server set the status code to 404?
ABoth Step 2 and Step 3
BStep 1
CStep 3
DStep 2
💡 Hint
Look at the 'Action Taken' and 'res.statusCode' in variable_tracker for Steps 2 and 3.
If we add a new condition for '/goodbye' to send 'Goodbye!', how would the execution table change for Step 2?
AServer crashes
BCondition result remains false and response is 'Not Found'
CCondition result becomes true and response is 'Goodbye!'
DResponse is empty
💡 Hint
Think about adding a new if block for '/goodbye' before the else, changing Step 2's action.
Concept Snapshot
Routing requests manually in Node.js:
- Create server with http.createServer
- Check req.url in callback
- Use if/else to match paths
- Send response with res.end()
- Set res.statusCode for errors
- Unmatched paths return 404 Not Found
Full Transcript
This example shows how to handle HTTP requests manually in Node.js. The server listens for requests and checks the URL path. If the path is '/hello', it sends back 'Hello World'. For any other path, it sets the status code to 404 and sends 'Not Found'. The key is to check req.url and respond accordingly. The response must be ended with res.end() to complete the request. This manual routing is the basic way servers decide what to send back based on the URL.