In the previous section, we built a single middleware to perform an action before or after the request hits the handler. It is also possible to chain a group of middleware. In order to do that, we should follow the same closure logic as the preceding section. Let us create a city API for saving city details. For simplicity's sake, the API will have one POST method, and the body consists of two fields: city name and city area.
Let us think about a scenario where an API developer only allows the JSON media type from clients and also needs to send the server time in UTC back to the client for every request. Using middleware, we can do that.
The functions of two middleware are:
- In the first middleware, check whether the content type is JSON. If not, don't allow the request to proceed
- In the second middleware, add a timestamp called...