I have a specific requirement that calls for setting a custom Request-Id
header for each axios request. The value of this header is generated dynamically by a separate express middleware.
While I could manually set the header like this:
axios.get(url, headers: {'Request-Id': req.requestId});
I prefer to abstract this operation to a common location for reusability. I have developed a custom express middleware for this purpose:
app.use(function (req, res, next) {
req.fetch = axios;
req.fetch.defaults.headers.common['Request-Id'] = req.requestId;
next();
})
With this middleware in place, I can now use req.fetch
in any route without the need to individually set the header each time. However, I am unsure if this is the best approach. Can someone provide insight into any potential drawbacks of this method, or suggest a better solution?