Introduction to Express.js
Express is the most widely-used web framework for Node — a thin, unopinionated layer over the raw http module that removes the boilerplate you wrote by hand in the previous section. Routing, body parsing, middleware, response helpers: Express turns dozens of lines of manual req/res handling into a few expressive calls. Crucially, it doesn't hide Node — it organizes it. Everything you learned about HTTP still applies.
The same server, raw vs Express
Compare a small JSON endpoint. First, raw http — manual URL parsing, method checks, status, and serialization:
Raw http
import { createServer } from 'node:http'
createServer((req, res) => {
const url = new URL(req.url, `http://${req.headers.host}`)
if (req.method === 'GET' && url.pathname === '/users') {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify([{ id: 1 }]))
} else {
res.writeHead(404).end('Not Found')
}
}).listen(3000)The same thing in Express
import express from 'express'
const app = express()
app.get('/users', (req, res) => {
res.json([{ id: 1 }]) // sets Content-Type + stringifies for you
})
app.listen(3000) // unmatched routes 404 automaticallyWhat Express gives you
Feature | Raw Node | Express |
|---|---|---|
Routing (method + path) | Manual |
|
Path parameters | Regex by hand |
|
Query parsing |
|
|
Body parsing | Read the stream yourself |
|
Response helpers |
|
|
Middleware pipeline | Hand-wired |
|
Static files | Build a safe server |
|
The mental model: middleware all the way down
Express's one big idea is the middleware pipeline. A request flows through an ordered chain of functions, each receiving (req, res, next). A function can respond, or modify req/res, then call next() to pass control onward. Route handlers are just middleware that match a method and path. We devote a whole section to middleware; for now, hold this picture:
Request │ ▼ [ logger ] → [ express.json ] → [ auth ] → [ route handler ] → Response (each calls next() to continue, or sends a response to stop)
Is Express still the right choice?
Express has been the default for over a decade — enormous ecosystem, endless tutorials, battle-tested. Alternatives exist with different trade-offs:
Framework | Angle |
|---|---|
Express | The standard — minimal, huge ecosystem, well-documented |
Fastify | Faster, schema-based validation, modern plugin system |
Koa | From the Express authors — async/await-first, tiny core |
NestJS | Opinionated, TypeScript, structure for large apps |
Hono | Ultra-light, edge/runtime-agnostic |