Skip to main content

Middleware

Middleware in Alapa provides a powerful mechanism for inspecting and filtering HTTP requests as they enter your application. Middleware is often used to handle common tasks such as authentication, logging, and modifying request or response objects before they reach the route handler.

Alapa's middleware system is inspired by Express, offering simplicity and flexibility for developers to implement custom and reusable logic.


Understanding Middleware

Middleware acts as a pipeline through which requests pass before they reach the defined route handlers. If a middleware detects an issue (e.g., the user is not authenticated), it can interrupt the request cycle and respond directly. Otherwise, it invokes the next() function to pass control to the next middleware or route handler.

Example: Basic Middleware

The following middleware handles requests to the /dashboard route:

src/app/dashboard/middleware.ts
import { Request, Response, NextFunction } from "alapa";

const dashboardMiddle = (req: Request, res: Response, next: NextFunction) => {
console.log("Middleware executed for /dashboard");
next(); // Proceed to the next middleware or route handler
};

export default dashboardMiddle;

Middleware Structure

Basic middleware functions take three arguments:

  • req: The request object.
  • res: The response object.
  • next: A function that passes control to the next middleware or route handler.

Applying middleware

To apply middleware to a route handler you can use either use or middleware methods on the Router's instance as given bellow.

src/app/dashboard/router.ts
import { Router } from "alapa";
import dashboardMiddle from "./middleware";

const dashboardRoutes = new Router();

dashboardRoutes.use(dashboardMiddle);
// OR
dashboardRoutes.middleware(dashboardMiddle);

Chaining Middleware

You can chain multiple middleware functions for a single route or group of routes.

Example: Multiple Middleware Functions

untitled
import { Router, Request, Response, NextFunction } from "alapa";

const loggerMiddleware = (req: Request, res: Response, next: NextFunction) => {
console.log(`${req.method} ${req.url}`);
next();
};

const checkAuthMiddleware = (
req: Request,
res: Response,
next: NextFunction
) => {
if (req.headers.authorization) {
next();
} else {
res.status(401).send("Unauthorized");
}
};

const router = new Router();
router.use("/secure", loggerMiddleware, checkAuthMiddleware);

Here, the logger middleware logs the request details before the authentication middleware validates the user.


Error-Handling Middleware

Middleware can also handle errors that occur during request processing. Error-handling middleware functions have an additional err parameter as the first argument.

Example:

untitled
import { Request, Response, NextFunction } from "alapa";
const errorHandler = (
err: any,
req: Request,
res: Response,
next: NextFunction
) => {
console.error(err.stack);
res.status(500).send("Something went wrong!");
};

router.use(errorHandler);

Summary

Middleware is a critical part of the Alapa framework, allowing you to implement reusable logic for tasks like authentication, logging, and error handling. Whether applied globally, to specific routes, or as part of a middleware chain, it provides a structured way to manage request processing in your application.