Is it possible to get the current request that is being served by node.js?

I am using express.js. I have a need to be able to log certain request data whenever someone tries to log a message. For this I would like to create a helper method like so

function log_message(level, message){
  winston.log(level, req.path + "" + message);
}

I would then use the method like so.

exports.index = function(req, res){
  log_message("info", "I'm here");
}

Note that I am not passing the req object to the log_message function. I want that to be transparently done so that the log_message API user does not need to be aware of the common data that is being logged.

Is there a way to achieve this with express.js/node.js. Is the request object available from a global variable of some sort?

An interesting way to do this would be the new Domains feature. http://nodejs.org/api/domain.html

Domains, while providing excellent error recovery, can be used as a type of "Thread Local Storage" - basically storing data for each request.

Create some middleware that adds every request/response to a domain.

app.use(function(req, res, next) {
  var reqd = domain.create();
  reqd.add(req);
  reqd.add(res);
  reqd._req = req; // Add request object to custom property
  // TODO: hook error event on reqd (see docs)
  next();
});

In the log function, you can now get the current domain and pull out the request object.

function log_message(level, message) {
  // Pull the request from the current domain.
  var request = process.domain._req;

  // TODO: log message
};

Domains are still experimental, but it doesn't sound like much will change between now and the 1.0 release.

How is the log_message exposed to the caller (a module, etc) and what control do you have over the pipeline preceding the route?

You could apply middleware preceding this route's invocation and have the function log_message come from within a closure, or you could take advantage of the req EventEmitter facility and wrap the call to winston.log inside a handler for the req.end and just log all messages that have been made during the request. This would effectively change your log_message to be an accumulator of log messages (maybe in an Array) and just log them all at the end of the request.

This all depends on how you are exposing this stuff though.

Many cats have been skinned here :)

Similar to the domains answer, it's now a lot easier to do this using continuation-local-storage: https://datahero.com/blog/2014/05/22/node-js-preserving-data-across-async-callbacks/

At DataHero we save a transaction id, user id, and session id with all log messages. You don't need to pass the request object all the way down, so it helps keep your models / business layer clean, too.

The following solution is acceptable to me.

In here I have a middleware which adds the log_message method onto the request object. After that I simply call req.log_message to log the message. While this is very similar to passing the req object to every logging call it's just slightly cleaner.

function logging_middleware(req, res, next){
    req.log_message = function(level, message){
        winston.log(level, req.path + ":" + message);
    }
    next();
}

create a midleware:

app.use(function(req, res, next) {
       var tid = uuid.v4();
     var cls = require('continuation-local-storage');
       var namespace = cls.createNamespace('com.storage');
      var pre_ip;
          if(get_ip(req))
          { ip_info= get_ip(req).clientIp;
              pre_ip=ip_info
          }
          namespace.bindEmitter(req);
          namespace.bindEmitter(res);
        namespace.run(function() {
               console.log(logobj);
              namespace.set('tid', tid);
              namespace.set('ip',ip_info);
           namespace.set('logobj',logobj);   
              next();
          });
      });

And use it :

var cls = require('continuation-local-storage');
 var namespace = cls.getNamespace('com.storage');
      namespace.get('ip');