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.

Implementing middleware

Implementing middleware

Now, let's understand how to implement middleware in NestJS. So first, let me create a folder, I'll name it as middleware. Inside this folder, I'll create a file called logging.middleware.ts. Here the dot middleware is the naming convention, but is not strictly necessary. It's more of a convention to make it clear that the file contains middleware code. Now inside the file, we will first provide the injectable decorator, as we are going to create a class and market as a middleware. And in order to use that class anywhere in the application, we give the injectable decorator. Let me create a class. I'll give export class and the class name LoggingMiddleware. Now in order to mark this class as a middleware, we will use an interface called NestMiddleware. So let me give implements NestMiddleware. So whenever we want to create a middleware, we always use the NestMiddleware interface. This interface offers a standard method that is used for specifically defining middleware logic, and that…

Contents