How it works...
Middleware can be used to perform simple operations such as logging, metric collection and analytics. It can also be used to dynamically populate variables on each request. This can be used, for example, to collect an X-Header from the request to set an ID or generate an ID, like we did in this recipe. Another ID strategy might be to generate a UUID for every request--this allows us to easily correlate log messages together and trace your request across different applications if multiple microservices are involved in building the response.
When working with context values, it's important to consider the order of your middleware. Typically, it's better to not make middleware reliant on one another. For example, in this recipe, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access