node.js make request and response global for HTTP server?
I've just started on node.js and ran through http://nodebeginner.org/index.html. Great start tutorial but I really want to know is it possible to make both request and response 'global' so any module loaded for the current incoming request can access these开发者_如何学Go... rather than injecting.
Ideas?
Unlike most web-scripting language, in Node many HTTP requests from different clients can be 'active' at the same time. How would you ever know which client you're responding to?
So while you think there's only 1 request and response at any given time, there are actually open requests for all current clients.
Due to the asynchronous event loop, it would be theoretically possible to make the reques and response varibale globally available… BUT, as soon as you return from your current context (even when calling other async stuff), the next event in the queue will be executed.
Now think of another HTTP client connecting in the meantime. It will change the global variables again and you will lose your old. So in the end, your approach is leaking.
Having only a single thread executing your own code, everything is obviously threadsafe. But you still have to protect against invalid states (variables etc.), because you never know what is the next event/callback to be executed.
Node Domains.
This is an old question, I realize that, but none of the answers are completely correct.
The truth is that you can achieve this functionality by using Node Domains.
Global variables are generally considered bad to use because they break encapsulation and a properly encapsulated application is the first building block to a good design because the application will be easier to read, test, and refactor.
That being said I, I have personally come across many good use cases for using global variables within the scope of a single request.
Example Tracking a single request through many layers of code becomes impossible without this, unless you expose your request (or req ID) to layers it just doesn't belong. (i.e. Service, DAL, etc... keep your request in your controller were it belongs).
Basically, Passing variables through many layers just so that they are accessible at the lower levels of my application is unsustainable and makes code very messy.
Before you start screaming, yes I am aware that Domains have been deprecated as of Node 5, and I am also aware that this is not the exact use case for Domains as it is documented. But the Node is finalizing a new API now, that will hopefully continue to solve this issue.
https://nodejs.org/api/domain.html
continuation-local-storage solves this problem. If I understand correctly, there are some similarities to domains, only, it's not deprecated. :D
This post explains it a bit. And related slideshow.
It should be possible, but that would require an array to hold the references to the response and request objects. And then you would have to tell the module the index where to find the objects in the array. Also you would have to cleanup the array. So you can't really avoid passing some info to the module.
Better pass the request and response object references directly to the module.
Keep in mind that objects are passed by reference, so there is not a big overhead when passing objects to functions/modules.
精彩评论