Skip to content

How to understand a Express codebase

Express apps are a chain of middleware. Almost everything that matters, from auth to validation to error handling, is a function registered with `app.use` or on a router, and the order of registration is the order of execution. Reading Express means reconstructing that chain.

How to tell it's Express

  • express in package.json
  • const app = express() followed by app.use(...) calls
  • express.Router() in route files and handlers shaped (req, res, next)

Where things usually live

src/app.ts or app.js
Creates the app and registers middleware and routers, in order.
src/server.ts or bin/www
Starts listening on a port; sometimes creates the HTTP server for websockets.
src/routes/
Routers mounted at path prefixes such as /api/users.
src/controllers/
Handler functions called by routes.
src/middleware/
Auth, validation, rate limiting, logging.
src/services/ or models/
Business logic and database access.
src/config/
Environment loading and settings.

Conventions, not rules. Real projects rename and move things, so confirm against the repository.

Where to start reading

  1. package.json main or start script, to find the real entry file
  2. The file that calls express(), reading every app.use from top to bottom
  3. The router mounted at the path you care about

How a request moves through the code

  1. 1The request passes through global middleware in registration order: parsers, CORS, logging, sessions.
  2. 2Express matches the path to a mounted router, then to a route on that router.
  3. 3Route-level middleware runs, such as auth or validation.
  4. 4The handler calls services, which query the database, and sends a response.
  5. 5If anything calls next(err) or throws, Express skips ahead to error-handling middleware (functions with four arguments).

Questions to ask about the repository

  • List the global middleware in the order it runs.
  • Which routes require authentication, and where is that enforced?
  • Trace POST /api/orders from the router to the database write.
  • Where are errors turned into HTTP responses?
  • Which environment variables does the server need to start?

What trips people up

  • Middleware registered after a router doesn't run for that router's routes; order is behaviour.
  • Express 4 doesn't catch rejected promises in async handlers, so many codebases wrap handlers or use a helper. Express 5 handles them natively; check the version.
  • A handler that neither responds nor calls next() leaves the request hanging, with no error.