keiro
also supports a middleware.ts
file which is called before each route, to define a middleware:
API Reference
Middlewares take 2 arguments:
- RequestEvent: Contains the
request
, url
, params
, cookies
and request locals
.
- Next: which represent the next handler in the chain.
And must return a Response object.
Middlewares can be defined in any of these ways:
-
Declaring a function.
-
Using the defineMiddleware
helper.
-
Using the Middleware
type.
Chaining multiple middlewares
You can use multiple middlewares in a row using the sequence
helper.
Examples
Logging middleware
Error Handler middleware
Timeout middleware
Auth middleware