How to chain middlewares in Express.js

Arguments

Middlewares take three arguments:

  • req: the request object
  • res: the response object
  • next: next is a function that passes execution to the next middleware when it is done.

Middlewares are used inside the app.use() or app.METHOD() functions, where METHOD refers to the HTTP verbs GET, POST, PUT, DELETE, and more.

Chaining middlewares

Middlewares can be chained. We can use more than one middleware on an Express app instance, which means that we can use more than one middleware inside app.use() or app.METHOD(). We use a comma (,) to separate them.

Code

Using commas to separate middlewares