NodeJSIntroduction to Express.js

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

JS
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

JS
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 automatically
Express removes ceremony, not understanding
Notice what disappeared: manual URL parsing, the method check, `writeHead`, `JSON.stringify`, the 404 fallback. `res.json()` sets the `Content-Type` and serializes; `app.get` matches method *and* path. But under the hood Express is calling the exact same `http` APIs — which is why knowing the raw layer makes Express click instantly.
What Express gives you

Feature

Raw Node

Express

Routing (method + path)

Manual if/switch

app.get/post/...

Path parameters

Regex by hand

/users/:idreq.params

Query parsing

new URL(...)

req.query (auto)

Body parsing

Read the stream yourself

express.json()req.body

Response helpers

writeHead + end

res.json/send/status/redirect

Middleware pipeline

Hand-wired

app.use(...)

Static files

Build a safe server

express.static(dir)

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:

Text
Request
   │
   ▼
[ logger ] → [ express.json ] → [ auth ] → [ route handler ] → Response
   (each calls next() to continue, or sends a response to stop)
Order matters — middleware runs top to bottom
Because the pipeline is ordered, *where* you register middleware determines when it runs. Body parsing must come before handlers that read `req.body`; auth must come before the routes it protects. This single rule — top-to-bottom execution — explains most Express behavior and most Express bugs.
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

Learn Express first regardless
Even if you later choose Fastify or Nest, Express's concepts — middleware, routing, `req`/`res` helpers — are the lingua franca that every other framework borrows from or contrasts against. It's the right foundation, and the skills transfer directly.
A note on Express 5
Express 5 changed async error handling and a few APIs
Express 5 (now the current major) improves on v4 — most importantly, a rejected promise or thrown error in an `async` route handler is now forwarded to your error middleware automatically (in v4 it would hang the request unless you caught it yourself). Some pattern-matching and removed-method APIs also changed. This section targets modern Express; if you hit a discrepancy with an old tutorial, check whether it assumes v4.
Next
Get it installed and a project set up: [Installing & Setting Up Express](/nodejs/express-install).