Maintaining query scope in Express / Node.js
I am using Express to expose a REST API in my Node.js application. When the REST request comes in, I pull 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. The obvious but clumsy way is to pass user information as parameters to all function calls. Is there a better way? So far I've found the following solutions, but I'm not sure if they are prime time ready:
- StrongLoop Zone Library : The docs say, "The zone library and documentation are still under development: there are bugs, missing features, and limited documentation."
- Continuation-Local Storage : Not sure if this will be included in Node.js. This issue at the end recommends looking at the StrongLoop zone.
- Node.js Domains : It doesn't look like this has really taken off.
source to share
I'm not sure what you are still looking for, but I built a while ago express-http-context
for this purpose. This is explicit middleware that sits on top of cls-hooks
(formerly continuation-local-storage
).
Full disclosure: This is an npm package that I have created and maintain.
source to share