DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Day 28 – Middlewares - Learning Node JS In 30 Days [Mini series]
Muhammad Ali (Nerdjfpb)
Muhammad Ali (Nerdjfpb)

Posted on • Originally published at blog.nerdjfpb.com

Day 28 – Middlewares - Learning Node JS In 30 Days [Mini series]

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next. (official document)

Do you understand anything of it ?

In short, normally we use middleware to checking something. Like – if anyone is a authentic user or is the user is admin.

Middleware functions can perform the following tasks:

  • Execute any code.
  • Make changes to the request and the response objects.
  • End the request-response cycle.
  • Call the next middleware function in the stack.

An Express application can use the following types of middleware:

  • Application-level middleware
  • Router-level middleware
  • Error-handling middleware
  • Built-in middleware
  • Third-party middleware

You can check the more details – https://expressjs.com/en/guide/using-middleware.html

Alt Text

Let’s use a middleware

Alt Text

we have 3 elements here – request, response and next cycle here. In the middleware we just console log the time. So when we’ll call the route, this function will be called always.

Alt Text

See the result when we hit the url –

Alt Text

You can see the graphical version here

Originally it published on nerdjfpbblog. You can connect with me in twitter or linkedin !

You can read the old posts from this series (below)

Top comments (0)

Take Your Github Repository To The Next Level

Take Your Github Repository To The Next Level πŸš€οΈ: A step-by-step guide on creating the perfect Github repository.