Documentation
Docs
Introduction
Getting Started
Tutorial: build an app
Core Concepts
Routing
Server-Side Rendering
Mimir, state management
Pages & Layouts
API Routes
Styling & Theming
Building & Deploying
API Reference
Guides
Recipes
FAQ (use cases)
Middleware
Middleware runs for every request before your page and API handlers. Register it with app.use(...). A
middleware is a (req, res, next) function: inspect or augment req, set headers or short-circuit on res,
then call next() to continue (or send a response and return to stop).
EkkoJS ships 13 production-ready middleware in ekko:web, so you rarely write the cross-cutting ones
yourself. Import what you need and app.use it:
Each middleware has its own page below, with a flow diagram, the use case, every option, and a copy-paste example. This page is the map: what exists, what order to register it in, and a recommended production stack.
The built-in middleware
| Middleware | What it does |
|---|---|
helmet | Security headers on every response. |
cors | Allow other origins to call your API. |
rateLimit | Throttle abuse / brute force by IP. |
bodyLimit | Reject oversized request bodies (DoS). |
validateContentType | Reject unexpected body content types. |
csrf | CSRF protection for cookie-auth writes. |
requestId | Correlate logs across a request. |
timeout | Return 504 for a hung request. |
errorHandler | Clean 500s + logging, no leaked stacks. |
httpsRedirect | Force plaintext requests to HTTPS. |
secureCookies | Make every cookie Secure by default. |
ipFilter | Allow / deny by client IP. |
safePath | Block .. path traversal app-wide. |
Order matters
Middleware runs in registration order, before route handlers. Put the broad, always-on concerns first (request id, security headers), the ones that may reject early next (rate limit, body limit, content-type, CSRF), and the error wrapper outermost:
A recommended production stack
Adjust to your app: drop cors if the front-end is this same rune app; add csrf for cookie auth; add
httpsRedirect / ipFilter where the topology calls for it.
Per-route middleware and options
Beyond app-wide app.use(...), you can scope middleware and limits to one route. Pass middleware
functions between the path and the handler, or use route options:
Route options the server understands include rateLimit: { max, window }, an auth scheme name, required
roles, and anonymous: true (skip auth). These let a generous global policy coexist with a strict
per-endpoint one (e.g. 300 req/min globally, but 5 login attempts/min).
Writing your own
The built-ins cover the common cases; write a custom middleware for app-specific logic, the most common
being authentication (attach req.user):
rune carries req.user into the page data as __user, so SSR and components can read the current user, see
Authentication. Short-circuit by writing a response and returning without
next():
Next: per-route options and automatic 405s, Validation & options.