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


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.
Subscribe to my newsletter
Read articles from Jatin Verma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
