From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Registering a middleware

Registering a middleware

Now, let's see how to register a middleware. Any middleware defined within NestJS has to be registered with a specific module. Right now, as we do not have any module other than the app module, let's register the middleware inside it. So inside the app module TS file, we have the app module class. This is where we have to configure the middleware. And to do so, we have to make use of an interface called nest module. This interface allows us to provide additional configuration to a module like configuring middleware. This interface provides a method called configure, which is used to register a middleware. And as you can see, it receives a middleware consumer parameter, which allows the module to configure middleware for incoming requests. The middleware consumer is actually a helper class that provides few built-in methods to be used with middleware. So the configure method is part of the Nest module interface and is implemented within module class, and it takes a middleware consumer…

Contents