# Middleware

Middleware provides a great way to inspect and filter HTTP requests entering your application. As an example, ExpressWebJs provides an auth middleware that verifies if the user of your application is authenticated. If the user is not authenticated, an error message with appropriate error code is sent.

Additional middleware can be written to perform a variety of tasks besides authentication. For example, a logging middleware might log all incoming requests to your application.

# Creating Middleware:

To create a new middleware, use the make-middleware maker command:

ts-node maker make-middleware CheckHeader
//Within App/Http/Middleware
"use strict";
import { Request, Response, NextFunction } from "Elucidate/HttpContext";
import HttpResponse from "Elucidate/HttpContext/ResponseType";

class CheckHeader {
  async handle(req: Request, res: Response, next: NextFunction) {
    if (req.header("appId") == "abcd") {
      return HttpResponse.BAD_REQUEST(res, { message: "Bad Request" });
    }
    await next();
  }
}
module.exports = CheckHeader;

# Middleware UpStream & DownStream:

When creating your middleware, you’ll need to decide if it runs before or after the request hits your route handler. This is done by defining the code before or after the middleware handle method’s
await next() call:

//Within App/Http/Middleware
"use strict";
class UpStreamExample {
  async handle(req: Request, res: Response, next: NextFunction) {
    //Your code
    await next();
  }
}
module.exports = UpStreamExample;

In the UpStreamExample middleware, your code will execute before the request gets to route. Example of DownStream example:

//Within App/Http/Middleware
"use strict";
class DownStreamExample {
  async handle(req: Request, res: Response, next: NextFunction) {
    await next();
    //Your code
  }
}
module.exports = DownStreamExample;

In the DownStreamExample middleware, your code will execute after the request gets to route. Your middleware code can also run before and after the request hits your route handler:

//Within App/Http/Middleware
"use strict";
class BeforeAndAfterExample {
  async handle(req: Request, res: Response, next: NextFunction) {
    //UpStreamCode
    await next();
    //DownStreamCode
  }
}
module.exports = BeforeAndAfterExample;

# Registering Middleware:

All middleware are registered inside the App/Http/kernel.ts file.

# Global Middleware

If you want a middleware to run during every HTTP request to your application, list the middleware class in the applicationLevelMiddleware property of your App/Http/Kernel.ts file.

// Within App/Http/Kernel.ts
 applicationLevelMiddleware: [
    "App/Http/Middleware/CheckForMaintenanceMode",
    "App/Http/Middleware/ConvertEmptyStringsToNull",
  ],

# Assigning middleware to Route:

If you would like to assign middleware to specific routes, you should first assign the middleware a key in your application's App/Http/Kernel.ts file in the routeMiddleware section.

// Within App/Http/Kernel.ts
routeMiddleware:{
    auth:"/App/Http/Middleware/Auth",
    booking:"/App/Http/Middleware/Booking",
};

Once you have registered your middleware in the App/Http/Kernel.ts file, you can now use the middleware key to assign middleware to a route:

Route.get($uri, $callback, ["auth"]);

You may assign multiple middlewares to the route like so:

Route.get($uri, $callback, ["auth", "booking"]);

You can also assign middleware to route group like so:

Route.group({ middleware: ["auth", "booking"] }, () => {
  Route.get($uri, $callback);
});