Node.js make a request and respond globally for an HTTP server?

I just started with node.js and ran http://nodebeginner.org/index.html . A great getting started tutorial, but I really want to know if it is possible to make both the request and the response β€œglobal”, so any module loaded for the current incoming request can access them ... instead of typing.

Ideas?

+4
source share
5 answers

Unlike most web scripting languages, in Node, many HTTP requests from different clients can be "active" at the same time. How would you know which customer you are responding to?

So, while you think that at any given time there is only 1 request and response, in fact there are open requests for all current clients.

+4
source

Because of the asynchronous event loop, it is theoretically possible to make varbale requests and responses globally accessible ... BUT, as soon as you return from your current context (even when other asynchronous files are called), the next event in the queue will be executed.

Now think about connecting another HTTP client. It will change the global variables again and you will lose your old one. Therefore, in the end, your approach is proceeding.

With only one thread executing your own code, everything is explicitly thread safe. But you still need to protect against invalid states (variables, etc.), because you never know what the next event / callback will be that will be executed.

+2
source

Node Domains.

This is an old question, I understand this, but none of the answers are completely correct.

In truth, you can achieve this functionality 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 for good design because it will make it easier to read, test, and refactor.

As I said, I personally came across many good examples of using global variables within a single query.

Example Tracking a single request through many levels of code becomes impossible without this, unless you expose your request (or req ID) to layers to which it simply does not belong. (i.e. Service, DAL, etc ... save your request in the controller, if it belongs).

In principle, passing variables through many layers just to be available at lower levels of my application is unstable and makes the code very dirty.

Before you start screaming, yes, I know that domains are deprecated from Node 5, and I also know that this is not an exact precedent for domains, as it is documented. But Node is finalizing a new API, which we hope will continue to solve this problem.

https://nodejs.org/api/domain.html

+2
source

continuation-local-storage solves this problem. If I understand correctly, there are some similarities with domains, only they are not outdated .: D

This post explains this a bit. And related slide show .

+1
source

This should be possible, but it requires an array to store references to the response and request objects. And then you will need to tell the module the index where to find the objects in the array. You will also have to clear the array. Thus, you cannot avoid passing some information to the module.

It is better to pass links to the request and response object directly to the module.

Keep in mind that objects are passed by reference, so there is no big overhead when transferring objects to functions / modules.

0
source

All Articles