πŸš€ From Node.js to Express: Building Web Servers the Smart Way

Jatin VermaJatin Verma
3 min read

When learning backend development, one of the first steps is setting up a web server. If you start with raw Node.js, you’ll quickly realize that while powerful, it requires a lot of manual setup. This is where Express.js comes in β€” a minimal yet flexible framework that makes working with Node much more productive.

In this article, we’ll walk through:

  • Creating a simple web server with Node.js vs Express

  • How routes and requests are handled

  • What middleware is (and why it’s magical)

  • Understanding URL parameters vs query strings


πŸ› οΈ 1. Creating a Simple Web Server with Node.js

Let’s start with plain Node.js:

// server.js
const http = require("http");

const server = http.createServer((req, res) => {
  if (req.url === "/" && req.method === "GET") {
    res.writeHead(200, { "Content-Type": "text/plain" });
    res.end("Hello from Node.js!");
  } else if (req.url === "/about" && req.method === "GET") {
    res.writeHead(200, { "Content-Type": "text/plain" });
    res.end("About Page");
  } else {
    res.writeHead(404);
    res.end("Not Found");
  }
});

server.listen(3000, () =>
  console.log("Server running at http://localhost:3000")
);

This works, but notice how we’re manually checking req.url and req.method β€” it doesn’t scale well when routes grow.


⚑ 2. Enter Express.js

With Express, the same server becomes far simpler:

// app.js
const express = require("express");
const app = express();

app.get("/", (req, res) => res.send("Hello from Express!"));
app.get("/about", (req, res) => res.send("About Page"));

app.listen(3000, () => console.log("Server running at http://localhost:3000"));

✨ Express handles routing, headers, and response formatting behind the scenes. We only define what happens when someone visits a route.


πŸ”€ 3. Creating Routes and Handling Requests

Express makes defining routes intuitive:

app.post("/login", (req, res) => {
  res.send("Login successful!");
});

app.put("/update/:id", (req, res) => {
  res.send(`Updated item with ID: ${req.params.id}`);
});

Here’s the request lifecycle inside Express:

Request β†’ Middleware β†’ Route Handler (Controller) β†’ Response

πŸ“Š Flowchart Representation:

   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β”‚   Client   β”‚
   β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
         β”‚ HTTP Request
         β–Ό
   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β”‚ Middlewareβ”‚ (e.g. auth, logging, JSON parsing)
   β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
         β–Ό
   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β”‚ Controllerβ”‚ (your route handler logic)
   β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
         β–Ό
   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β”‚  Response β”‚ (HTML, JSON, etc.)
   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🧩 4. What is Middleware?

Middleware functions are like interceptors β€” they sit between the request and response.

Example: logging middleware

app.use((req, res, next) => {
  console.log(`${req.method} ${req.url}`);
  next(); // hand over control
});

Common uses of middleware:

  • Authentication

  • Request body parsing (express.json())

  • Error handling


πŸ”Ž 5. URL Parameters vs Query Strings

When sending data in a request, you often use either parameters or query strings.

URL Parameters

  • Part of the route itself

  • Accessed with req.params

  • Example:

app.get("/users/:id", (req, res) => {
  res.send(`User ID is ${req.params.id}`);
});

Request:

GET /users/42

Response:

User ID is 42

Query Strings

  • Appended to the URL after ?

  • Accessed with req.query

  • Example:

app.get("/search", (req, res) => {
  res.send(`You searched for: ${req.query.q}`);
});

Request:

GET /search?q=nodejs

Response:

You searched for: nodejs

🏁 Wrapping Up

  • Raw Node.js is powerful but verbose.

  • Express makes routing, middleware, and request handling much easier.

  • Middleware is the secret sauce β€” reusable and composable.

  • URL parameters (req.params) and query strings (req.query) are two common ways of sending data.

πŸ‘‰ Whether you’re building a small API or a full-scale web app, Express provides the structure and simplicity you’ll thank yourself for later.

0
Subscribe to my newsletter

Read articles from Jatin Verma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jatin Verma
Jatin Verma