# 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:

node maker make-middleware CheckHeader
//Within App/Http/Middleware 
"use strict";
class CheckHeader {
  async handle(req, res, next) {
    if (req.header("appId") == "abcd") {
      res.status(400).json({ 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, res, next) {
    //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,res,next){
     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,res,next){
     //UpStreamCode
     await next();
     //DownStreamCode
  }
}
module.exports = BeforeAndAfterExample

# Registering Middleware:

All middleware are registered inside the App/Http/kernel.js 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.js file.

// Within App/Http/Kernel.js 
 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.js file in the routeMiddleware section.

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

Once you have registered your middleware in the App/Http/Kernel.js 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('/prefix',['auth','booking'],()=>{
    Route.get($uri, $callback);
});

Note: In group route, the middleware comes before the callback function. Also you can define middleware to individual routes in route group

Route.group('/prefix',['auth'],()=>{
    Route.get($uri, $callback,['booking']);
});