In the previous section, we built a single middleware to perform an action before or after a 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 in the preceding section. Let's create a cityAPI program for saving city details. For simplicity's sake, the API will have one POST method, and the body will consist of two fields: city name and city area.
Let's us think about a scenario where a client is only allowed to send a JSON Content-Type request to an API. The main function of the API is to send a response to the client with a UTC timestamp cookie attached to it. We can add that content check in the middleware.
The functions of the two middleware are as follows:
- In the first middleware, check whether the content type is JSON. If not, don&apos...