Type Alias Middleware

Middleware: ((req: Request, next: (() => Promise<Response>)) => Promise<Response>)

Type of middleware function that processes the request before or after the main route handler.

Type declaration

    • (req, next): Promise<Response>
    • Parameters

      • req: Request

        The HTTP request object.

      • next: (() => Promise<Response>)

        A function to call the next middleware or route handler.

          • (): Promise<Response>
          • Returns Promise<Response>

      Returns Promise<Response>

      A promise resolving to an HTTP response.

      Example of a middleware function:

      import { start, router, text, type Middleware } from "@pulsar-http/core";

      const logMiddleware: Middleware = async (req, next) => {
      const response = await next();
      console.log(`Request ${req.method} ${req.url} - Response ${response.status}`);
      return response;
      };

      const routes = [
      router.get("/", async () => text("Hello, World!")),
      ];

      start({ routes, middlewares: [logMiddleware] });

      In this example, the logMiddleware will wait for the next middleware or route handler to finish processing the request, log the request and response information, and then return the response.