Done a little bit of digging and found out the undocumented
`process.domain`, which actually allows one to access the current domain
from anywhere. This is very similar to "thread local storage", but it's way
awesomer because it automatically traverses async calls.

For example (node 0.8.2):

```
var domain = require('domain');
var http = require('http');

function do_some_async_stuff(callback) {
  setTimeout(function() {
    console.log('url of current request is', process.domain &&
process.domain.url);
    return callback();
  }, 500);
}

var server = http.createServer(function(req, res) {
  var d = domain.create();

  // attach `req.url` to the domain as arbitrary context
  d.url = req.url;

  d.run(function() {
    do_some_async_stuff(function(err) {
      res.end('done');
    });
  });
});

server.listen(5000);
```

This is an *undocumented* *feature* so I would advise not to use it before
someone from node core gives their blessing.

Any thoughts from the core maintainers on this? Why did you guys choose not
to document it? Any caveats that people should be aware of?

Cheers,
Elad.

On Wed, Jul 4, 2012 at 7:03 AM, Gaurav Vaish <gaurav.va...@gmail.com> wrote:

> Hi Elad,
>
> > As far as I know node does not support something like that. One idea that
> > came to mind is to add support for associating arbitrary context to a
> > domain<http://nodejs.org/docs/latest/api/all.html#all_domain> and
> > extract it along the way.
> >
> > I was thinking on trying to hack something like that. Any thoughts?
>
> Do you already have some thoughts on the same?
>
>
> --
> Happy Hacking,
> Gaurav Vaish
> www.m10v.com
>
>


-- 
Elad.

Reply via email to