Middleware in the Express.js

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:

  • Execute any code.
  • Make changes to the request and the response objects.
  • End the request-response cycle.
  • 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.

To load the middleware function, call app.use(), specifying the middleware function.

For example, the following code loads the myLogger middleware function before the route to the root path (/).

  const express = require('express')
  const app = express()

  const myLogger = function (req, res, next) {
    console.log('LOGGED')
    next()
  }

  app.use(myLogger)

  app.get('/', (req, res) => {
  res.send('Hello World!')
  })

  app.listen(3000)