Let's get started with express Middle words.
We have two types of middle words.
One is custom materials.
That is materials that we are going to create by ourselves.
And we also have third party middleware where we can download from NPM registry.
So in this video, we are going to work with custom media words.
So back to Visual Studio Code.
I have already created a simple express server with few lines of code.
And I'll provide this file for you.
But if you want to use your custom express server, you can go ahead and do so.
So let's go through this code line by line.
We have the first route, which is home route, which sends hello word to the user.
The next route is a logging route.
And this one send logging successful.
And the next route is a route to create post.
And all what is doing is just to send post created.
And for the description we have row which is authenticated user.
Meaning that this route you need to be logged in before you can use this route.
Well, we have not talked about authentication and authorization yet, but let's try to mimic.
All right.
So it means that this route you need to log in before you can create a post.
And the next route is to fetch all posts.
And for description, it says that public meaning that whether you have logged in or not, you can still
have access to this route.
And the last route is to delete a post.
So for this action, unless you are an admin before you can delete a post.
So how can we achieve all this logic?
Well, we can do so using what is called middleware.
So how can we make use of middleware?
The first step is we need to create it.
So let's have a route map.
The first step is what creates middleware.
I start.
So let's go over again.
What is middleware?
So middleware is a function that have access to the request and the response object.
And if you have access to the request and response object, it means that we can do whatever we want
with it before we send the response or before getting to the server.
So here we go.
Since middleware is a function, we can use normal function syntax to create middleware.
So the middleware that we are going to create is called logger, which means that any time a user visit
any of our root, we want to display a message to the console.
So let's see how we can create that.
For this one, I'm going to use a function.
You can use normal functions syntax.
So let's go.
The first step is the name of the middleware, and I'll call it as logger and equal to my error function.
And this is a normal function syntax.
The next step is the argument that this function takes For many words.
It takes in three arguments.
The first one is a request.
Second one is response, and the next one is called next.
So these three arguments, the first one represents the request the user is making, and then the response
is what is going to be sent back to the user?
And the next is a function.
Do you remember we've talked about the next function in the previous video.
So the next function is a function that allows another murderer to proceed in the pipeline or in the
chain.
And I'll show you in just a second.
So inside the function body here, we can make use of any logic here.
For the meantime, we are just console.log in a message.
Says that a logger middle.
Where.
So now we have created our first middleware.
How can we use it?
So let's have a comment.
Says Use middleware.
So how can we use it and express make it pretty simple to plug in modules.
And here you go.
We make use of app on that.
We have what is used and the app is an instance of express and we have a method called use.
So you simply means that we are plugging in or we are using another function, which is what middleware
to enhance the functionality of our server.
So with upload use, we can plug in any function where we can add our own logic into it.
And the first one is called the Major world.
So we pass in the logger middleware and don't call it as this, but instead what use is going to call
the logger for us, and that is our murderer.
So now when I save it, make sure that your server is up and running.
And let's get back to Postman and I have created a collection called Middleware.
And inside I have one request to fetch our post.
So I'm trying to hit this route called Fetch or Post.
So let's scroll down here and this is a route I want to hit.
So now have a look when I hit send.
Now you can see something going on here.
Two things are going on here.
First one is we are not getting the response from the root and we see what is called sending requests.
The next step is if you check our terminal, we have a message that I'm a logger, which means that
our middleware has been called, but we are not getting the response from the root.
So why is it so?
Well, the first point is that any time you make a request to any of our root, this middleware function
will be called.
The next step is that unless we manually tell, express that please proceed to the next network.
That is why the next function is very important here, or it shines here.
So here to be able to allow Express to proceed on to the next middleware we are going to call next.
And this function is given to us by express automatically and we can name it whatever you want.
It can be anything.
But by convention we make use of next.
So when I save it now, when I cancel the request, now you see that our server has hang out.
Now let's make the request again.
And you can say that now I get my response and I also get my middleware message.
So now you can see how important the next function is.
So the next function allows experts to move on to the next middleware.
So we be asking, we have only one middleware.
So what is the next middleware?
Remember, the root handle is and this is our root handler.
Let me show you after our path here, we have the callback function and this is our request handler
and is a middleware also.
So the first step is that as soon as we hit that root, first is going to call the middleware, this
middleware here.
And then if it is next here, then Express will push on to the next middleware, which is this root
that you called, which is this root that we call this one.
So this also a middleware.
All right.
So now this is how you create a murderer.
So in the next video, let's go ahead and create a mature where it can display a message that please
log in before you can access it.
Let's get into that in the next video.