Node.js: Code Outside the Browser / HTTP Module-Creating a Web Server (No Express)

Basic routing using if-else or switch blocks

🧭 What Is Routing?

Routing means deciding how your app responds to different requests made to different URLs.

  • http://localhost:3000/ β†’ Home page

  • http://localhost:3000/services β†’ Services page

  • http://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 Page

http://localhost:3000/services

Status: 200 OK
Body:   πŸ’Ό Our Services include Web Dev and Design

http://localhost:3000/contact

Status: 200 OK
Body:   πŸ“ž Reach us at 123-456-7890

http://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 clean case per route

  • 🌱 Beginner-friendly β€” keeps you from nesting if/else blocks 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-Type if 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 .html file and send its contents as the response.