Middleware

Middleware

  • 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

  • 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 is essentially a series of middleware function calls

  • Here’s an example of one type of Middleware that can be found in an Express application

  app.get('/user/:id', function (req, res, next) {
    res.send('USER')
  })
  
  • The code above uses the .get() middleware to listen for GET requests to /user/:id