π§ What Is Routing?
Routing means deciding how your app responds to different requests made to different URLs.
http://localhost:3000/β Home pagehttp://localhost:3000/servicesβ Services pagehttp://localhost:3000/contactβ Contact page
Whenever a request comes in for a specific path, the server needs to know what to send back β that decision-making is routing.
βοΈ How Routing Works in Node.js
Node's built-in http module gives every request handler access to req (the request) and res (the response).
Checking req.url tells you which page the visitor is trying to reach, so you can respond accordingly. You can make that decision with either if/else or switch β same job, different style.
π΄ if-else Version
// if-else-server.js
import { createServer } from 'node:http';
const server = createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
if (req.url === '/') {
res.write('π Home Page');
} else if (req.url === '/services') {
res.write('πΌ Our Services include Web Dev and Design');
} else if (req.url === '/contact') {
res.write('π Reach us at 123-456-7890');
} else {
res.statusCode = 404;
res.write('β 404 - Page Not Found');
}
res.end();
});
server.listen(3000, () => {
console.log('if-else server running at <http://localhost:3000>');
});π’ switch Version
// switch-server.js
import { createServer } from 'node:http';
const server = createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
switch (req.url) {
case '/':
res.write('π Home Page');
break;
case '/services':
res.write('πΌ Our Services include Web Dev and Design');
break;
case '/contact':
res.write('π Reach us at 123-456-7890');
break;
default:
res.statusCode = 404;
res.write('β 404 - Page Not Found');
}
res.end();
});
server.listen(3000, () => {
console.log('switch server running at <http://localhost:3000>');
});Both versions route the exact same three pages, so you can compare them directly.
π» Terminal output (either version):
if-else server running at <http://localhost:3000>switch server running at <http://localhost:3000>π Browser output β visiting each route:
http://localhost:3000/
Status: 200 OK
Body: π Home Pagehttp://localhost:3000/services
Status: 200 OK
Body: πΌ Our Services include Web Dev and Designhttp://localhost:3000/contact
Status: 200 OK
Body: π Reach us at 123-456-7890http://localhost:3000/unknown
Status: 404 Not Found
Body: β 404 - Page Not Foundπ€ Why Use switch?
π§Ή Neater β easier to scan once you have more than 2β3 routes
π« No repeated checks β one
switch(req.url), then a cleancaseper routeπ± Beginner-friendly β keeps you from nesting
if/elseblocks too deeply
Both approaches work identically β switch just tends to stay readable as the route list grows, while a long if/else chain gets harder to scan.
π Live Example Flow
Visiting localhost:3000/services:
Request comes in β req.url is '/services' β matches the '/services' case
β Responds: "πΌ Our Services include Web Dev and Design"Visiting localhost:3000/unknown:
Request comes in β req.url is '/unknown' β no case matches
β Falls through to default β Responds: "β 404 - Page Not Found" (status 404)β οΈ Don't Forget
β Always call
res.end()after writing content β without it, the browser keeps waiting and the page never finishes loading.π·οΈ Always set
Content-Typeif you're sending HTML or JSON β for plain text it's optional, since that's the default.π To serve real HTML pages later,
fs.readFile()lets you load an.htmlfile and send its contents as the response.