Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Routing requests manually
📖 Scenario: You are building a simple Node.js server that handles different web page requests manually without using any routing libraries.This is like being a traffic controller who directs cars (requests) to the right destination (response) based on the road signs (URL paths).
🎯 Goal: Create a Node.js server that listens on port 3000 and responds with different messages for the paths /, /about, and /contact. For any other path, respond with a 404 message.
📋 What You'll Learn
Create a basic HTTP server using Node.js http module
Add a variable to store the port number 3000
Use if statements to check the request URL and send different responses
Send a 404 response for unknown paths
💡 Why This Matters
🌍 Real World
Manual routing is the foundation of web servers. Understanding it helps you grasp how frameworks like Express work behind the scenes.
💼 Career
Many backend jobs require knowledge of how HTTP servers handle requests and routes, even if you use frameworks. This skill helps in debugging and customizing server behavior.
Progress0 / 4 steps
1
Create a basic HTTP server
Write code to import the http module and create a server using http.createServer that takes req and res parameters. For now, respond with "Hello World" for every request.
Node.js
Hint
Use require('http') to import the module. Then create the server with http.createServer((req, res) => { ... }).
2
Add a port variable
Create a constant variable called port and set it to 3000.
Node.js
Hint
Use const port = 3000; to store the port number.
3
Add routing logic for different paths
Inside the server callback, use if statements to check req.url. If it is "/", respond with "Home Page". If it is "/about", respond with "About Page". If it is "/contact", respond with "Contact Page". For any other URL, respond with "Page Not Found" and set status code to 404.
Node.js
Hint
Use if and else if to check req.url and respond accordingly.
4
Start the server listening on the port
Add code to make the server listen on the port variable. Use server.listen(port).
Node.js
Hint
Use server.listen(port); to start the server.
Practice
(1/5)
1. What is the main purpose of checking req.url in a Node.js server without frameworks?
easy
A. To decide how to respond to different request paths
B. To parse the request body automatically
C. To connect to a database
D. To handle user authentication
Solution
Step 1: Understand the role of req.url
req.url contains the path requested by the client, like '/home' or '/about'.
Step 2: Purpose of checking req.url
By checking req.url, the server can decide which content or response to send back.
Final Answer:
To decide how to respond to different request paths -> Option A
Quick Check:
Routing = Check req.url [OK]
Hint: Check req.url to route requests manually [OK]
Common Mistakes:
Thinking req.url parses request body
Confusing req.url with database connection
Assuming req.url handles authentication
2. Which of the following is the correct way to send a plain text response with status 200 in a manual Node.js server?
easy
A. res.status(200).send('Hello');
B. res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello');
C. res.sendStatus(200).send('Hello');
D. res.write(200, 'Hello');
Solution
Step 1: Identify correct methods for manual response
In manual Node.js servers, res.writeHead() sets status and headers, res.end() sends the response body.
Step 2: Check each option
res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello'); uses writeHead and end correctly. The second and third options use Express methods, not native Node.js. res.write(200, 'Hello'); is invalid syntax.
Final Answer:
res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello'); -> Option B
Quick Check:
Manual response = writeHead + end [OK]
Hint: Use writeHead and end to send manual responses [OK]
Common Mistakes:
Using Express methods in plain Node.js
Calling res.write without headers
Incorrect method names like res.sendStatus
3. What will the following Node.js server code output when a client requests /hello?