HTTP Fundamentals

Middleware

Filter requests through global middleware, aliases, route middleware, and parameters.

Beta · v0.2.0Lanjit APIs may change before the stable release. Review the release notes and upgrade guide before updating.

Register middleware aliases and global middleware in bootstrap/app.js. Route middleware may be chained and parameterized, such as throttle:10,1. The starter application includes session, CSRF, CORS, authentication, throttling, powered-by, and security-header middleware.

Examples

node artisan make:middleware EnsureTeamMember
export class EnsureTeamMember {
  async handle(request, next, response) {
    if (!request.user?.team_id) {
      return response.json({ message: 'Team required.' }, 403);
    }

    return next();
  }
}
.withMiddleware((middleware) => {
  middleware.alias({
    team: EnsureTeamMember,
    auth: AuthenticateMiddleware,
  });

  middleware.append(SecurityHeadersMiddleware);
})
Route.get('/team', handler).middleware('session', 'auth', 'team');
Route.post('/api/import', handler).middleware('throttle:10,1');
Edit this page on GitHub

Type to search all documentation.