01 / 04

What are middlewares?

Middleware functions are functions that have access to the request object (req), the response object (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:
  1. 1

    Execute any code.

  2. 2

    Make changes to the request and the response objects.

  3. 3

    End the request-response cycle.

  4. 4

    Call the next middleware in the stack.

If the current middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging.