Introduction
In this article we understand why we need Middleware, this is in general of backend. In amy backend you see there is middleware and I will talk about the real use of it
In this article I am talking in general about the middleware.
I will also talk some of the common question which is asked regarding middleware ?
- Is middleware mandatory?
- Why middleware is important ?
- When to use middleware?
I have a video where I talked the same thing which I am is written in this article. In vide I had shown snippet of laravel to make things more clear.
What is middleware?
Middleware is bridge between the request handler
and actual request which reach to the server.
You can see the image here can understand it.
Now you understand where
Middleware in real application
I am going to give some real world example where middleware is being used.
Questions
- Is middleware mandatory?
No middleware in not mandatory to use. You can work without using middleware also. If you use the middlewre then you will find that you work become easy.
- Why middleware is important ?
It makes our work easier, we can attach middleware to our web application anytime with one line of code. If you don’t use the middleware then you will give you pain. If you want to get user IP of all the address then you can assume that how much code you need to write in all the route.
- When to use middleware?
Whenever you see that you are doing some kind of thing which is common in some route then you can use the middleware. Like: Verify is user is authenticated or not, get the website hits (view count) etc
- When to use middleware?
You can almost everything using the route but you should not use middleware to replace the job of actual route handler. You can return the response to user using middleware also but you should never do that. Let router handler handle the route request. In future if you got into the bug then you have to update the middleware. You should avoid using middleware for one route.
Thanks for coming to my blog and reading. If you like it then please let me via tweet or comment.
Tweet #nitishk72_