FACG6 / CMSPosts

Link :
https://yourwordsapp.herokuapp.com/
0 stars 0 forks source link

What is a middleware? #21

Open NouraldinS opened 5 years ago

NouraldinS commented 5 years ago

Firstly, a middleware is not a controller/router/route, meaning that: A. The folder middlewares should not be inside controllers B. No middleware should have a res.send or any similar function call

A middleware is a function that is called to use/modify the request's object, it could be for validation or statistics collection, it could be for logging, or anything but ending the server/client connection. A middleware should always pass to the next middleware unconditionally, even if it passes an error to the next middleware/route.

Also, some of your middlewares could be mushed together, like checkIncomeData.checkInsert, checkEmail.check and checkIncomeData.checkLogin, checkEmail.checkLogin, comparePassword.compare, createCookie.cookie These groups could be one middleware that does the task and returns an error if something wrong happened. Definitely yes splitting your code like that does help in code readability, but sometimes the readability of some code wouldn't be affected (or even enhanced) if you combined some of your functions together to serve a greater purpose.

My words could sound a bit conflicting, but aside from all what I said, often look at your code and ask yourself: "Is this my best?", and then do better.