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.

Applying global middlewares

Applying global middlewares

Now, let's see how to apply middlewares globally. There are basically two ways we can apply any middleware globally in NestJS. Let's check it out. I have middleware here, which we implemented in the previous lecture. To apply this method globally, I'll go to the app module file, where we have defined the for routes method. And inside it, I'll give the path property, setting a wildcard route and method property where I give the request method dot all. The all represents all the HTTP methods that is get, put, patch, post, delete, etc., thus setting the middleware globally. So if I try to make the request in the postman, we already have the request body here. Let me make the post request and it works perfectly. So currently the middleware is globally applied. Now the second way is a more familiar way. If you have experience dealing with middleware in Express, that is by using the app.use method. What I'll do here, I'll comment on this apply method, and we don't need any consumer here in…

Contents