- [Instructor] What is a middleware?
In the context of our known
and expressed server, how would it be used?
Middleware are simply functions that have
access to the request and response object
in our express application and can run their code there.
It can make changes to the request response objects,
can end them, can call another function
in the stack using next, et cetera.
We'll do an example when it routes
with middleware at one of the endpoints.
Let's get to it.
So if you don't have it open you want to open
the CRM route file which is in the routes folder.
And then what we're going to do is add middelware
in the get method right here.
So what we're going to do first is pass
to the arguments next.
And this is something we have access to in express
and it allows us to call in to another function
once we executed the first one.
So now we're going to transform this into
a full function here and then let's just close that out
and put the parenthesis here and remove it from there.
And then place this inside of our function like so.
Where we're going to introduce the middleware is right here,
so let's go ahead and put a note
and then we'll insert some functions right here.
So the first thing we're going to do is add a console log
to user request object and print out
some information about the get method
or about the request from the front end or postman.
So what we're going to do is pass request from,
and then we're going to use the template strings
to pass from the request object original URL.
So basically we'll know where is this URL
or this request coming from
and then we're going to create another one.
So let's create another one where we do
exactly the same thing.
And as opposed to do request from we'll do request type,
and then pass template strings and then
use the request object to pass the method,
so what is the method of that request.
In theory it should be a get but let's
have it from the request object.
And then we're going to use the next option
to pass to the next function.
So what we need to do is close out our function here,
like so, and then put comma,
and then pass the request, response,
next, and then do another function
which will close out right here like so.
And the next function is the response.
So let me recap what's happening here.
So we're using the next function from the express library
to use middleware in the middle of the get request,
so we're basically giving information about
what is this request from and what is this request method
and then we're using next to pass to the next function
where we do the response directly to the front end.
So let's go ahead and save that and test that,
so we don't have any issues with our server here,
so we're good, let's go back here,
and let's do a get request.
And let's remove the specific ID here.
And we're going to send, and we get our response here.
But if we go back to Visual Studio Code,
in the console log we have information about the request,
so the request is from the contact URL
and the request type is from the get method.
So as you can see we can add anything we want
as middleware into our endpoints.
It could also be a third party library
where you want to do something special,
your imagination is your limit.