Skip to main content

Production Express Guide

Concept​

Express provides a minimal routing and middleware model over Node HTTP; production quality comes from explicit validation, errors, security, architecture, tests, and operations.

Why It Exists​

Express remains flexible, but flexibility can turn into implicit middleware order and inconsistent boundaries.

Mental Model​

Treat every arrow as a boundary with a cost, ownership rule, cancellation behavior, and failure mode. Node.js is effective when those boundaries are explicit instead of hidden behind framework defaults.

How It Works​

The JavaScript callback runs on the main JavaScript thread. Native Node.js bindings, libuv, the operating system, worker threads, child processes, or remote services may perform work elsewhere. Completion only becomes useful when control returns to JavaScript. Under load, the important questions are what is queued, what is bounded, what can be cancelled, and which resource saturates first.

Example​

import express, {type ErrorRequestHandler} from 'express';

const app = express();
app.use(express.json({limit: '1mb'}));
app.get('/health', (_request, response) => response.json({status: 'ok'}));

const errors: ErrorRequestHandler = (error, _request, response, _next) => {
console.error(error);
response.status(500).json({type: 'about:blank', title: 'Internal Server Error', status: 500});
};
app.use(errors);
app.listen(3000);

The example is intentionally small enough to execute, but the production boundary is the important part: validate inputs, establish a deadline, propagate cancellation, classify failures, and release resources deterministically.

Production Use​

Organize by feature or bounded context, keep controllers thin, inject services, use central validation/error policy, and configure proxy trust intentionally.

Security​

Apply security headers, authn/authz, CSRF where cookies are used, rate limits, safe uploads, body limits, and redacted logs.

Performance​

Middleware order adds per-request cost and semantic coupling. Profile validation, serialization, database waits, and synchronous middleware.

Common Mistakes​

  • Putting business logic in routes.
  • Trusting req.user without object-level authorization.
  • Registering error middleware before routes.

Debugging​

Print the route/middleware stack in development, add request IDs, and isolate whether latency is in middleware, service, or dependency.

Testing​

Use unit tests for services, integration tests with real dependencies, and HTTP tests for middleware order, errors, auth, and shutdown.

When Not to Use It​

Do not choose Express when schema-driven serialization, encapsulated plugins, or a stronger application framework is a core requirement.

Interview Questions​

  • How does Express middleware ordering work?
  • How do async errors reach error middleware?
  • How would you structure a large Express codebase?

Official References​