|
| 1 | +# Middleware Guide |
| 2 | + |
| 3 | +Arkstack provides a structured approach to middleware configuration and execution. This guide covers where to define middleware, the execution order, and best practices for layering. |
| 4 | + |
| 5 | +## Where middleware is configured |
| 6 | + |
| 7 | +Define middleware in your kit's `src/config/middleware.ts` file. |
| 8 | + |
| 9 | +Each config returns the same shape: |
| 10 | + |
| 11 | +```ts |
| 12 | +{ |
| 13 | + global: [], |
| 14 | + before: [], |
| 15 | + after: [], |
| 16 | +} |
| 17 | +``` |
| 18 | + |
| 19 | +## Middleware execution order |
| 20 | + |
| 21 | +Arkstack applies middleware in this order during `app.boot(port)`: |
| 22 | + |
| 23 | +1. Public assets are mounted. |
| 24 | +2. `global` middleware runs. |
| 25 | +3. `before` middleware runs. |
| 26 | +4. Routes are bound. |
| 27 | +5. `after` middleware runs. |
| 28 | +6. Runtime-specific startup continues (Express error handler registration, then server start). |
| 29 | + |
| 30 | +Use each group with this intent: |
| 31 | + |
| 32 | +- `global`: middleware that should always run for every request (body parsing, CORS, method override, baseline security headers). |
| 33 | +- `before`: middleware that should run before route handlers are bound/executed (request context setup, auth gates, request-level tracing). |
| 34 | +- `after`: middleware that should run after routes are bound (post-route transforms, tail processing). |
| 35 | + |
| 36 | +## Express example |
| 37 | + |
| 38 | +`express/src/config/middleware.ts`: |
| 39 | + |
| 40 | +```ts |
| 41 | +import express, { Express } from 'express'; |
| 42 | +import cors from 'cors'; |
| 43 | +import methodOverride from 'method-override'; |
| 44 | +import { MiddlewareConfig } from 'src/types/config'; |
| 45 | + |
| 46 | +const config = (_app: Express): MiddlewareConfig => { |
| 47 | + return { |
| 48 | + global: [ |
| 49 | + express.json(), |
| 50 | + express.urlencoded({ extended: true }), |
| 51 | + methodOverride('X-HTTP-Method'), |
| 52 | + cors(), |
| 53 | + ], |
| 54 | + before: [], |
| 55 | + after: [], |
| 56 | + }; |
| 57 | +}; |
| 58 | + |
| 59 | +export default config; |
| 60 | +``` |
| 61 | + |
| 62 | +## H3 example |
| 63 | + |
| 64 | +`h3/src/config/middleware.ts`: |
| 65 | + |
| 66 | +```ts |
| 67 | +import { H3 } from 'h3'; |
| 68 | +import { MiddlewareConfig } from 'src/types/config'; |
| 69 | +import { cors } from '@app/http/middlewares/cors'; |
| 70 | + |
| 71 | +const config = (_app: H3): MiddlewareConfig => { |
| 72 | + return { |
| 73 | + global: [cors()], |
| 74 | + before: [], |
| 75 | + after: [], |
| 76 | + }; |
| 77 | +}; |
| 78 | + |
| 79 | +export default config; |
| 80 | +``` |
| 81 | + |
| 82 | +## Route-level middleware |
| 83 | + |
| 84 | +Use config middleware for app-wide concerns. For route-specific behavior, attach middleware at route declaration time. |
| 85 | + |
| 86 | +Example from Express route registration: |
| 87 | + |
| 88 | +```ts |
| 89 | +import { Router } from 'src/core/router'; |
| 90 | + |
| 91 | +Router.get( |
| 92 | + '/', |
| 93 | + ({ res }) => { |
| 94 | + res |
| 95 | + .setHeader('Content-Type', 'text/html') |
| 96 | + .send('Welcome to the Express application!'); |
| 97 | + }, |
| 98 | + [ |
| 99 | + () => { |
| 100 | + console.log('Middleware for root route'); |
| 101 | + }, |
| 102 | + ], |
| 103 | +); |
| 104 | +``` |
| 105 | + |
| 106 | +## Team expectation |
| 107 | + |
| 108 | +- Prefer `src/config/middleware.ts` for cross-cutting concerns. |
| 109 | +- Keep `global` minimal and predictable. |
| 110 | +- Put request gating/initialization in `before`. |
| 111 | +- Reserve `after` for true post-route concerns. |
| 112 | +- Keep route-level middleware close to the route when the concern is route-specific. |
0 commit comments