As the lead for many Express 5 services, which Express defaults and ecosystem middleware would you make mandatory in a shared bootstrap, and what does that cost?
answer
- Express ships no hardening
- Express defaults that hurt in production
- one factory, per-service config
- mandatory versus opt-out
basics
~20 sExpress defaults to x-powered-by on, env 'development' without NODE_ENV, trust proxy false and no security middleware. A shared bootstrap fixes those and mounts helmet, cors where needed, express-rate-limit and a health route; TLS and bulk compression usually stay at the proxy.
solid answer
~40 sStart from what Express does when nobody configures it: it sends `X-Powered-By: Express`, runs as `'development'` unless `NODE_ENV=production` is set before `express()` is called, reports the balancer as `req.ip` because `trust proxy` is `false`, and ships no helmet, CORS, rate limiting or compression. A shared bootstrap, a versioned package with a function every service calls, should set `trust proxy` from platform configuration (never `true`), disable `x-powered-by`, mount `helmet()` with a per-service CSP hook, `cors` with an explicit allow-list only for cross-origin callers, an `express-rate-limit` instance on a shared store, and a cheap health route ahead of the limiter. TLS termination, bulk compression and edge rate limits usually belong to the proxy. The cost is coupling every service to one package's upgrades, which teams accept for consistent defaults.
go deeper
Recall that Express ships no security middleware and that x-powered-by is on and trust proxy off by default.
Explain what NODE_ENV=production changes inside Express, why it must be set before express() runs, and what each package in the baseline adds.
Show you can turn the baseline into one bootstrap function with configuration for trust proxy, CORS origins and limits, and verify it with header tests.
Own the split between app and edge, the mandatory-versus-opt-out policy, and the upgrade coupling a shared package creates across many services.
## Start from Express's own defaults Express is deliberately minimal, so a production baseline is mostly a list of defaults that are wrong for a deployed service: | Express default | What it does in production | Baseline choice | |---|---|---| | `x-powered-by` enabled | every response says `X-Powered-By: Express` | `app.disable('x-powered-by')` | | `env` from `NODE_ENV`, falling back to `'development'` | no `view cache`; the default error handler's page shows stack traces | the platform sets `NODE_ENV=production` before the process starts | | `trust proxy` `false` | `req.ip` is the balancer, `req.secure` is false behind TLS termination | a hop count or subnet list from platform config, never `true` | | no security headers | no CSP, no HSTS | `helmet()`, with a hook for each service's CSP | | no CORS headers | cross-origin browser calls fail | `cors` only for services with cross-origin callers | | no rate limiting | unlimited requests per client | `express-rate-limit` with a shared store | | `express.static` `maxAge` `0` | every asset revalidated | long cache only for fingerprinted assets | Two details make `NODE_ENV` a platform job rather than a code change: Express reads it when `express()` creates the app and stores it as `app.get('env')`, so setting it later in code does not reach that app; and `NODE_ENV=production` turns on the `view cache` setting. How the default error handler hides stacks belongs with the error-handling material, and what `NODE_ENV` means to Node itself and to package installs belongs with the Node runtime material. ## What goes in every app A shared bootstrap is usually **one versioned package exporting a function** such as `applyBaseline(app, config)`, called first in each service: 1. `app.disable('x-powered-by')` and `app.set('trust proxy', config.trustProxy)`, where the value is owned by whoever owns the network path; 2. a health route such as `GET /healthz` answering `res.sendStatus(200)` and registered **before** the limiter and authentication; what it should check is the health-monitoring lesson's subject; 3. `helmet()` with the defaults, plus a way for HTML-serving services to pass CSP directives, while JSON-only APIs keep the defaults; 4. `express-rate-limit` with `standardHeaders: 'draft-8'`, `legacyHeaders: false`, a shared `store` when there is more than one instance, and limits from configuration, because the library's defaults of 5 per minute fit almost no API; 5. `cors`, mounted only when `config.corsOrigins` is non-empty, with an explicit origin list and `credentials` decided per service. What each package's options mean is covered by the questions on each package; the principal question is which ones are mandatory and which are configurable. ## What the proxy does better Some hardening is cheaper and more consistent at the edge: - **TLS termination** and certificate renewal, which is why `trust proxy` matters at all; - **bulk compression**: Express's performance guide recommends compressing at the reverse proxy for high-traffic sites; in the app, the `compression` middleware skips bodies whose known size is under its `1kb` default `threshold`, skips responses marked `Cache-Control: no-transform`, prefers Brotli when the client accepts it, and needs `res.flush()` for server-sent events, so running both layers wastes CPU; - **coarse rate limits and connection limits** that protect every service before Express runs; - **static assets**, often served by the proxy or a CDN instead of `express.static`. The app keeps the controls that need application knowledge: limits keyed by user or API key, per-route CORS, and the CSP that only the service's authors can write. ## The tradeoffs a lead owns - **Consistency versus coupling.** One package means one place to fix a bad default, but every service now upgrades with it; semantic versioning and a changelog matter. - **Mandatory versus opt-out.** Making helmet and `trust proxy` mandatory catches the common mistakes; letting services opt out of a header needs a reviewed escape hatch, or the baseline erodes. - **Edge versus app duplication.** Rate limiting in both places is defensible, since they protect different things; compressing in both is not. - **Verification.** A short header-assertion test in each service, checking that `X-Powered-By` is absent, the helmet headers are present and `req.ip` resolves correctly behind the real proxy, keeps the baseline honest after upgrades. Draining connections on shutdown and running several processes per host are runtime concerns handled with the Node runtime material, not by this bootstrap.
- Should compression run in the Express app or at the proxy?For high-traffic services, Express's performance guide recommends the reverse proxy, which compresses for every service without spending Node's event-loop time. Keep the `compression` middleware for services with no compressing proxy, remembering its 1kb `threshold`, its skip on `Cache-Control: no-transform`, and `res.flush()` for event streams. Never compress at both layers.
- How do you stop a shared hardening package drifting from what services actually run?Version it, make each service call one exported function first, and add a small header-assertion test per service that checks for the helmet headers, the missing `X-Powered-By`, the rate-limit headers and a correct `req.ip` behind the real proxy. A failing test after an upgrade or an infrastructure change surfaces the drift before production does.
- Set trust proxy to true in the shared package so it works behind any proxy.
- Express ignores NODE_ENV, so setting it is only about npm installs.
- Compression should always run in the app, even when the proxy already compresses.
- One global CORS allow-list in the shared package fits every service.
- The health route should sit behind the rate limiter and authentication like any other route.