Middlewares are used for tasks which are shared across your app handlers like
validating the incoming request, logging,
authentication, rate-limiting, etc.
Router → Middleware Handler → Application Handler
In case you have such a list of middlewares, you can wrap/chain them with your server handler.
Below is a small example of middleware wrapping.
Live demo here
The output being :
first middleware!
second middleware!
server resp: hello from backend server!
Notice the sequence of execution. The call gets resolved based on the ordering of the
middlewares (left to right).