Will you?

Today was one of those days. You know. The kind where nothing goes as planned, the kind where you eventually get it done but none of it goes smoothly. I’m sure all of us have been there right? As I…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Understanding ExpressJs middlewares for beginners

While learning application development in expressJs, I was overwhelmed with the term called middlewares. Though I am not an expert in this category, from my experience, I think if you really want to achieve express supremacy, you need to know it all about middlewares.

As this article is targeted for beginners, I will start from very basic. So first let’s start from understanding request and response objects.

Whenever a user interacts with any web application, he/she sends a request to a server in the form of an object. A server receives the request and sends a response accordingly.

Now for the server to provide a response, it needs to perform some operations on the request and response object. This care of operation duties is taken by middlewares.

Middlewares are just functions that have access to the request object (req), the response object (res) and can therefore perform any operations on request and response objects. They run between the client and the server answer.

Let’s understand them more deeply using a car assembly line example. On the assembly line, at every station, there will be operations that need to be done. For example, at station 2, headlights fitments are to be done whereas, at station 10, wheels are to be fitted. At the last station, your car is completely assembled and is ready to go for testing.

In our example, care of operations is taken by assembly stations. Similarly, in express application, middlewares act as assembly stations so they care of operations. Middlewares works with your request and response object just like stations did with your cars and after performing operations, it provides you with an assembled response.

There is still one thing that is remaining to fully understand middlewares.

Now in our car assembly example, our car was moving from one station to another via conveyor. But how a express application will move from one middleware to another?

The answer is next(). Whenever operations are finished performing, one needs to call next() function to call the next middleware if there is any. This next movement needs to be continued unless and until a response is sent.

Also, If the current middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging.

Now I hope you know the concept of middlewares, let’s see them in action.

We’ll be creating private routes with the help of middlewares.

We will first create a node server using express.

After creating a server, let’s make authentication middleware. Now for the sake of simplicity, I am not creating a real-time authentication function. Instead, I will be using simple console.log statements.

We will use the above function as a middleware in our private route

To understand what I have written, first, let’s see a response on a node server.

So now that we have our response, let’s understand what it means. As you can see our authenticated function is getting executed first and then the private route. It means that by passing authenticated function as a middleware to a private route, it performs authentication operation first and then response comes to the private route which is executed later courtesy of next().

This way you can create a chain of as many middlewares you want.

There is also another way of using middlewares. The one I used here is called route level middleware. It means that this way of using middleware works for that specific route only. But let’s say our application consist of all private routes i.e you only want to give access to authorised users. Though using the above approach will also work but there is a more refined solution for this kind of needs.

Whenever there is a need of using middleware on application level i.e throughout the application we use app.use().

app.use() will add the middleware on an application level. We don’t need to pass the middleware as a parameter to privateRoute1 or privateRoute2 in current function and everything will work just fine. This approach is used whenever you need a middleware to be used throughout the application for every route.

There is a lot more to using middlewares like error handling middlewares, third-party middleware, built-in middleware and much more. I hope, I have given you the foundation so that you can explore them yourself.

By any means, you need more help regarding middlewares or you’re having any difficulty, you can always leave a message in a comment’s section. Also, suggest any changes or corrections or any piece of advice, I will be very happy to work on them.

Add a comment

Related posts:

The Antichrist revealed

They showed the devil’s works in the beginning, and then God cause him to crawl on his belly, for all time, as a snake. But all the killing and murder and destruction was being done by your God, the…

Sem conversas vazias cheirando a vinho barato

Ela queria que eu a acompanhasse naquela maldita festa. Falou que seria diferente daquelas mesmas confraternizações onde se distribuíam palavras vazias cheirando à vinho barato. Não acreditei, mas…

Autumn Leaves

The air is getting crisp. “Autumn Leaves” is published by Kelsey Rae in Literally Literary.