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.
Route-specific middleware - Nest.js Tutorial
From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework
Route-specific middleware
Generally, when we apply a middleware in NestJS, it is applied globally for all routes. But what if we want to apply middleware only to specific routes? To understand that, let's take a look at an example. I have created a token middleware and I have given the use method. Now first of all, above the use method, I'm going to create a private read-only array and naming it as valid tokens, which will be of type string array. I'll define a few tokens in the array, random token 1, 2, and 3. Now inside the use method, I'm going to create a token constant. We are going to pass the token inside the headers. So we need to retrieve that token from the HTTP request headers. In token based authentication, the token is typically passed in the request headers, specifically in the authorization header. So let me assign request dot headers dot authorization. Now to check if the token is correct, I'll create a private method isValidToken, which will take a single argument token of type string and the…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
(Locked)
Introduction to Nest middlewares3m 46s
-
(Locked)
Implementing middleware3m 45s
-
(Locked)
Registering a middleware5m 20s
-
(Locked)
Route-specific middleware5m 48s
-
(Locked)
Assignment: Checking Content-Type with middleware4m
-
(Locked)
Handling route wildcards3m 19s
-
(Locked)
Middleware for specific route handlers4m 38s
-
(Locked)
Excluding routes3m 49s
-
(Locked)
Controller-driven route middleware2m 15s
-
(Locked)
Understanding functional middleware4m 26s
-
(Locked)
Applying multiple middlewares6m 51s
-
(Locked)
Applying global middlewares3m 52s
-
(Locked)
Assignment: Password encryption middleware10m 19s
-
(Locked)
-
-
-
-
-
-
-
-