node.js make request and response global for HTTP server?
Asked Answered
E

5

5

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... rather than injecting.

Ideas?

Euphonium answered 31/8, 2011 at 20:3 Comment(0)
S
5

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.

Sizable answered 31/8, 2011 at 20:5 Comment(2)
What you said is correct, but you can actually use node domains to achieve what he is asking. Please see post below.Isostasy
I think this answer just restates the question. There are options. Domains and continuation-local-storage. To be fair, I'm not sure if either of these options were available at the time this was posted.Exorbitance
H
2

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.

Hama answered 31/8, 2011 at 20:30 Comment(0)
I
2

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

Isostasy answered 29/2, 2016 at 19:23 Comment(0)
E
1

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.

Exorbitance answered 22/3, 2016 at 20:18 Comment(0)
P
0

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.

Prodigal answered 31/8, 2011 at 20:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.