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.

Middleware for specific route handlers

Middleware for specific route handlers

We can also apply the middleware for specific route handlers like GET, POST, PUT, PATCH or DELETE. Let's see how to achieve that. Currently, in the example, all the routes falling under the client route are being watched by the middleware. Let's say we have a post route handler. After all these get handlers, I'll give the post handler and the method name as route four, which returns a message. This is route four under slash client. Now let's say we want the middleware to be applied on all the get handlers and exclude the post handler. So when this kind of situation arises, we define the route inside the for routes method in a different manner. I'll remove the path and we'll give an object instead. In this object, we have to specify two properties. First is the path, which takes the route path. I'll give client slash and the asterisk sign. Also the path property takes the URL path patterns or basically route paths. It supports basic route patterns as well as wildcard patterns using the…

Contents