简体   繁体   中英

filter outgoing requests in node.js for logging

I am building an Express app which on certain requests has to make its own HTTP calls. I could use Superagent, request or node's own http.request . Thing is, I need to log all of those server originating requests and their respective responses. Calling log.info before each and every of those seems silly.

How can you add a pre-filter for all outgoing HTTP calls, and ideally access both req and res ?

NOTE: I am not interested in logging requests coming in to the server I am building, only in the requests that the server itself kicks off. Think of my server as a client to another black box server.

What you can do is patch http and https and proxy the request method. This way you can have a global handler that will catch the req & res objects.

var http    = require('http');
var https   = require('https');

var patch = function(object) {
    var original = object.request;

    // We proxy the request method
    object.request = function(options, callback) {
        // And we also proxy the callback to get res
        var newCallback = function() {
            var res = arguments[0];

            // You can log res here
            console.log("RES",res.statusCode);

            callback.apply(this,arguments);
        }

        var req = original(options, newCallback);

        // You can log your req object here.
        console.log(req.method,req.path);

        return req;
    }
}

patch(http);
patch(https);

http.get("http://www.google.com/index.html", function(res) {
  console.log("Got response");
}).on('error', function(e) {
  console.log("Got error: " + e.message);
});

Edit: This might work if you use the request npm package as well, as it might just rely on the built-in node.js http.request method anyways.

What server are you going to use for you app? I would definally bring up such functionality on to server level. Take a look how heroku router is doing it. You can track all of needed information using some of their addons: papertrail, or newrelic ( or use them separately for you app ).

https://papertrailapp.com/

http://newrelic.com/

I like out-of-box solutions in this case, no need extend your app logic for logging such information.

If you want to have your own solution, you can setup nginx to monitor request/response info. http://nginx.com/resources/admin-guide/logging-and-monitoring/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM