LibraryActive

AdonisJS Middleware

Middleware layer in AdonisJS that runs functions during an HTTP request before it reaches the route handler.

Open source page

Product features

Product features

Applying the middleware to routes

Conditional middleware execution

Middleware execution can be made conditional.

Creating and using middleware

Creating the authorization middleware

Dependency injection in middleware

Generating the middleware

Extending the HttpContext

Add custom properties to the context and augment the HttpContext type.

Implementing the logging logic

make:middleware command

Generates a scaffolded middleware class in the app/middleware directory.

Middleware

Middleware stacks

Three stacks: server, router, and named, each executing at different points in the request lifecycle.

Named middleware with parameters

Named middleware can accept parameters for per-route customization.

Named middleware with parameters

Registering the middleware

Registering the named middleware

Response modification

Middleware can add headers in the upstream phase and transform the response body.

Understanding middleware execution flow