23 Nest JS How to use middleware in Nest JS and it's concept

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys today in this video we are going to look at one more concept inside nest js which is middleware now this is not something which is specific to nesgus but rather you'll find this in any framework a middleware as the name suggests is something which sits in between the client request and your route handlers so when the browser makes an http request to any of your route inside our application before it can hit the controller and start executing the code if we want to make any checks and balances right then we use middlewares so for example in this video what we are going to try to do is create a middleware which will allow us to check whether there is an api token in our header or not this is very common practice if you are using some sas products where they give you a token and they say that you know if you want to consume our apis then you to pass this token in your header so that we can authorize that the request which is coming to our server is from a valid resource so yeah let's get started but before we start i would like you to hit the thumbs up icon and promote this video and yes if you're not already subscribed then do click on the subscribe button as well so let's get started so the middleware which we are trying to create may not directly go inside any of the modules so i will go inside my comments and in there i'll create a folder called middleware and inside that folder i'll create a file called api token check middleware something like this okay let me close out the terminal for some time maybe i'll also start the application so that it's there yeah i think this is fine now let's export a class called api token check middleware this needs to implement the nest middleware okay and now if you see it is complaining that there is some problem if you hover over it it says that the property used is missing in this right which means we need to implement that property or rather that method over here so let us do that and as you can see we are already able to get what are the parameters and stuff like that a few changes obviously i would like to do the request is of type request from express so i will just type in that this one similarly the response is also something which is coming from express and this is also a next function which we will import from express like so what is the expectation from any middleware the thing is middleware is like a pipeline okay so you have first middleware then you have second third so on and so forth okay now when the request comes in from the client right like this let's say it hits the first middleware now if for some reason the first middleware feels that whatever is the request is fine it doesn't need to bother or it doesn't have any problem it will call the next function and then what is going to happen is the second middleware will get called but now just imagine that for some reason the second middleware says that there is a problem let's say the problem is what we are trying to do which is the api token which is coming to the application is not a valid api token so what do we do at this point if we don't call the next function then it is not going to go ahead in the pipeline okay rather it will stop over here and throw an exception and return the response and that is how this middleware works okay so what do we want to do then we are getting the request object the request object is quite big but what i already know is that if i do console.log request dot headers i will see certain headers which are coming to our api route but obviously at this point we will not see the console i'm sorry about that because we haven't told nest js what to do with this middleware right so it's there it's a file which is lying around inside of a common folder but how will nest know that it needs to do something with it so let's go to our app module inside that we need to write a little bit of code first of all it needs to implement nest module now the moment we implement the nest module it would request us to add the configure method so let's do that as you can see it is giving this has a parameter which is consumer okay it is saying it is declared but never used that's fine we will use it okay and inside the consumer what we can do is we have a method called apply and in that i can say which middleware to apply so this is how i'm going to do it but then it also needs to say for which routes and for now let's just say we are going to do that for path which is star but then apart from path it would also require the method and we can say all or we can even say post let's just go with all right now okay the app is running and so far so good now let's just try to use the app controller route which just prints out the hello world so i'll quickly go over here new request i don't remember it was it 3001 or 3002 i think 3 000 let's see right it is 3 000 but i know what is the problem can you see it is still processing the reason is i made a mistake over here the next function needs to be called if you don't do that your response is never going to come it is go it is going to get inside the loop because it doesn't know what to do after you know going inside this middleware it is not throwing an exception it is not going calling the next function so it goes confused fair enough we have solved the problem so this time if i make a request this time we get the hello world and you can see we got the headers which are over here so why don't we add one more header let's just say this time our header name will be api token and over here i'll pass something let's just say my token okay so now hit request send and i can see this key over here right so now things become very simple we remove the console what we are going to do is say request dot headers api token if it's equal to my token if it is not equal to my token okay throw new bad request exception and say the token doesn't match something like this now how do you get this my token is really up to you you can query a database and check you can you know maybe call some third-party api whatever it is right that's your business logic but what we are going to do over here is we are going to check that whether the request header is present or not and if it is not then we throw one bad exception so let's try and see this time nothing happened that's fine but if i do my token one can you see it says token doesn't match okay and if i hit send again and if i don't send this header again it is giving me a bad request with the message so as you can understand the middleware is now allowing us to check our business logic correct and if it fails then it is not going to execute the other middleware that's about it guys that's how we use middlewares in our application to check some kind of business logic like here we are checking whether the api token is correct or not and whether it is present or not as well if you like this video do click on the thumbs up icon and don't forget to subscribe to my channel
Info
Channel: Amitav Roy
Views: 11,254
Rating: undefined out of 5
Keywords: nodejs, nestjs, javascript, jwt, api, api security
Id: 8vyXWyml634
Channel Id: undefined
Length: 9min 39sec (579 seconds)
Published: Tue May 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.