Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 252
Kosmotaur Avatar asked Oct 31 '22 06:10

Kosmotaur


1 Answers

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.

like image 54
Tristan Foureur Avatar answered Nov 13 '22 20:11

Tristan Foureur