Skip to content

Middleware

Middleware provides a convenient way to filter HTTP requests entering your application. This includes authentication middleware, which can be used to restrict access to certain parts of the application only to logged-in users. Additionally, you can create custom middleware to perform various tasks.

INFO

Middleware is typically placed in the src/http/middleware directory.

Creating Middleware

Generate Middleware

You can use the Sauf command to create a new middleware:

sh
$ npx sauf make:middleware OnlySecure
sh
$ pnpx sauf make:middleware OnlySecure
sh
$ bunx sauf make:middleware OnlySecure

Laratype will create a new middleware file in the src/http/middleware/ directory named OnlySecure.ts.

TIP

You can create multiple middleware at once by passing multiple names.

See more
sh
$ npx sauf make:middleware OnlySecure LogRequests
sh
$ pnpx sauf make:middleware OnlySecure LogRequests
sh
$ bunx sauf make:middleware OnlySecure LogRequests

Writing Middleware

ts
import { Middleware, MiddlewareHandler } from "@laratype/http";

export class OnlySecure extends Middleware {
  handle: MiddlewareHandler = async (request, res, next) => {

    if (request.protocol() !== 'https://') {
      return response({
        message: 'Only secure connections are allowed.'
      }, 403)
    }

    const result = await next(request)

    return result;

  }
}
ts
import { RouteOptions, controller } from "@laratype/http";
import { BaseController } from "../src/http/controllers/BaseController";
import { OnlySecure } from "../src/http/middleware/Middleware";
import RegisterController from "../src/http/controllers/RegisterController";

export const baseRouteApi: RouteOptions = {
  path: "",
  middleware: [
    OnlySecure
  ],
  controller: controller(BaseController, 'home'),
  method: "get",
  children: [
    {
      path: '/register',
      controller: controller(RegisterController, 'register'),
      method: "post",
      request: CreateUserRequest,
    },
  ]
}

Middleware declared in the middleware array will be executed in order from top to bottom before the request is passed to the controller. If middleware returns a response, the processing will stop and the response will be sent back to the client immediately. If the middleware calls the next() function, the request will continue to be processed by subsequent middleware or the controller.

TIP

You can apply middleware to groups by using children in the route definition.

Commands

You can check the list of registered routes and related middleware using the Sauf command:

sh
$ npx sauf route:list
sh
$ pnpx sauf route:list
sh
$ yarn sauf route:list
sh
$ bun sauf route:list