I am using Express to expose a REST API in my Node.js application. When a REST request comes in, I extract the user information from the HTTP headers. I would like this information to be available throughout the life of this request, no matter what function I am in. An obvious but kludgy way is to pass around the user information as parameters to all function calls. Is there a better way? So far I have found the following solutions, but I am not sure if they are ready for prime time:
I'm not sure if you're still looking, but a while back I built express-http-context
for this very purpose. It's an express middleware that sits on top of cls-hooks
(formerly continuation-local-storage
).
Full disclosure: this is an npm package that I built and maintain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With