mtee

Margaret W.N

Posted on July 22, 2020

Middleware

Middleware is like a middle man in a business transaction, the only difference is middleware saves us time and money unlike middle men. It's a term coined to refer code that executes in between sending a request to a server and receiving a response. It is one way to adhere to the DRY(Don't Repeat Yourself) software development principle.

In express we use app.use ( ) to leverage on middleware function calls. We pass an arrow function to .use ( ) that takes three parameters req, res and next where next is a callback function. The callback function fires the next operation and passes the data from the arrow function.

app.use((req, res, next) => {});
Enter fullscreen mode Exit fullscreen mode

So now any redundant code can be bundled up in the arrow function body, and executed in between processing the request.

πŸ’– πŸ’ͺ πŸ™… 🚩
mtee
Margaret W.N

Posted on July 22, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Day 23 of 100DaysOfCode
javascript Day 23 of 100DaysOfCode

October 3, 2024

Day21 of 100DaysOfCode
javascript Day21 of 100DaysOfCode

August 22, 2024

Day22 of 100DaysOfCode
javascript Day22 of 100DaysOfCode

September 7, 2024

Day 2 of #100daysofMiva Coding Challenge
100daysofmiva Day 2 of #100daysofMiva Coding Challenge

August 22, 2024