**Middleware **— In contrast to vanilla Node.js, where our requests flow through only one function, Express has a middleware stack, which is effectively an array of functions.

Writing middleware for use in Express apps

In Node, request, and response these two objects are passed through just one function. But in Express, these objects are passed through an arrayof functions, called the middleware stack. Express will start at the first function in the stack and continue in order down the stack

Middleware functions are functions that have access to the request (req), the response (res), and the next function in the application’s request-response cycle. The next function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware.

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 in the stack.

function(request, response, next)

request —An object that represents the incoming HTTP request.

**response **— An object that represents the outgoing HTTP response.

next — A function that will be going to the next middleware when called.

#nodejs #middleware

Middleware
1.85 GEEK