Routing requests manually means deciding what to do based on the web address a user visits. It helps your server send the right response for each request.
You want to handle different web pages or API endpoints without using extra libraries.
You are learning how web servers work by controlling requests step-by-step.
You need a simple server that responds differently based on the URL path.
You want to customize how your server reacts to specific URLs or HTTP methods.
Syntax
Node.js
const http = require('http');
const server = http.createServer((req, res) => {
if (req.url === '/') {
// handle root path
} elseif (req.url === '/about') {
// handle about page
} else {
// handle 404not found
}
});
server.listen(3000);
Use req.url to check the requested path.
Use res.writeHead() and res.end() to send responses.
Examples
Responds with 'Home page' when the root URL is requested.
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('Page not found');
Sample Program
This server listens on port 3000. It sends a welcome message for the root URL, contact info for '/contact', and a 404 message for other URLs.
Node.js
const http = require('http');
const server = http.createServer((req, res) => {
if (req.url === '/') {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Welcome to the Home page!');
} elseif (req.url === '/contact') {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Contact us at contact@example.com');
} else {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('404: Page not found');
}
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});
OutputSuccess
Important Notes
Check req.method if you want to handle GET, POST, or other HTTP methods differently.
Manual routing is simple but can get hard to manage for many routes; frameworks help with this.
Summary
Manual routing uses req.url to decide how to respond.
Use res.writeHead() and res.end() to send responses.
This method is good for learning or small servers without extra tools.
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?