Skip to content

Latest commit

 

History

History
51 lines (37 loc) · 1.27 KB

File metadata and controls

51 lines (37 loc) · 1.27 KB

Middleware

1 What Does a Middleware Do?

Middleware is a function runs before controller dividing by route.

It have the same parameter ability as the controller action.

2 Middleware

@Middleware({order: 0})
class ELKMiddleware implements IMiddleware {

  public use(@Next() next: Express.NextFunction) {
    // start tracking
    next()
    // end tracking
    // send to elk
  }
}

The class decorated by @Middleware will automatically wire to application. In this example, ELKMiddleware should run as the first middleware, just add an option {order: 0} to ensure it run first.

Middleware also support baseUrl options to apply middleware to a group of routes

@Middleware({baseUrl: '/admin'})
class AdminMiddleware implements IMiddleware {}

The example shows add a middleware for the admin routes.

3 ErrorMiddleware

@ErrorMiddleware have the same usage with @Middleware, one different is that in the middleware use function, you can get the error object by using @Err decorator.

@ErrorMiddleware()
class ExceptionHandler implements IMiddleware {

  public use(@Err() err: any, @Res() res: Express.Response) {
    console.log(err.message);
    res.sendStatus(400);
  }
}