Tuesday 12 June 2018

Express: Forward custom headers received from client to microservice server

HiI'm developing an an Express server that aggregates data from multiple microservices (by calling multiple APIs) to give client an aggregated response - so that the client does not need to make multiple API calls and that can be handled by the aggregator Express server.Now, the client sends some custom header like for auth, device info (for logging). These headers are processed by the microservices to authenticate requests, do some logging etc. So, the aggregator Express server just needs to pass along these custom headers while making API call to the microservice.My question is what is a good approach to pass received headers to an upstream server. Passing `req.headers` from router to every function right to my apiService (where I put my http request method calls) seems too repititive and dirty in code.Does some global request level context exists in Express, so I can avoid passing the req object around?I know global variables are evil, but passing around req object everywhere also just doesn't feel right.Please share what you feel is rigth to do in this scenario. Also share your experience if you have come across the same problem before.Thanks. Any help is much appreciated!

Submitted June 12, 2018 at 12:12PM by manishbhatt94

No comments:

Post a Comment