Concepts
Middlewares
A middleware it will intercept a request in order for you to execute custom logic before the route handler. Often, a middleware is used for things like authentication or logging. A middleware is a _middleware.ts|js
file.
// routes/_middleware.ts
export async function handler(ctx: Context, next: Function) {
// do something
await next();
}
Multiple middlewares
In case you need multiple middlewares, you can export the handler
as an array. In this example, middleware1
will be called first, then midleware2
.
// routes/_middleware.ts
export const handler = [
async function middleware1(ctx: Context, next: Function) {
// do something
await next();
},
async function middleware2(ctx: Context, next: Function) {
// do something
await next();
},
];