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
expressinpackage.jsonconst app = express()followed byapp.use(...)callsexpress.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
package.jsonmainorstartscript, to find the real entry file- The file that calls
express(), reading everyapp.usefrom top to bottom - The router mounted at the path you care about
How a request moves through the code
- The request passes through global middleware in registration order: parsers, CORS, logging, sessions.
- Express matches the path to a mounted router, then to a route on that router.
- Route-level middleware runs, such as auth or validation.
- The handler calls services, which query the database, and sends a response.
- If 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/ordersfrom 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.