Home » Nodejs » how to log OUTGOING https requests from node within webstorm

how to log OUTGOING https requests from node within webstorm

Posted by: admin November 29, 2017 Leave a comment

Questions:

I’m hacking together some node.js code that calls an external webservice and I’m getting bad results. I’d like to log the raw request and response so that I can inspect it.

Here’s the thing: I’m not consuming the http library directly, I’m consuming it through an OAuth library.

I’m already adding debug statements in the oauth library code and I don’t like it. Now it looks like I’m going to have to go into http library and start messing with that? This can’t be correct.

If I was on windows, I’d fire up fiddler. A friend mentioned wireshark but wireshark tells me I have to install X11. Really? I’m not going down that rabbit hole.

Then I tried node-inspector, but I think that is for server code not client code. It says your suppose to start your node process before attaching. Well my node process is a test case (vows) that ends shortly after is starts… so no luck there.

I guess this would difficult with any stack but jeez, it makes me miss .net!

So, how can I inspect what’s going over the wire when using node.js as client to external webservice on mountain lion?

thanks!
Dan

Answers:

Managed to install a hook on http/https request for the same reason.

function requestLogger(httpModule){
    var original = httpModule.request
    httpModule.request = function(options, callback){
      console.log(options.href||options.proto+"://"+options.host+options.path, options.method)
      return original(options, callback)
    }
}

requestLogger(require('http'))
requestLogger(require('https'))

Questions:
Answers:

You can check out the global-request-logger module, which uses the same technique that @uiron mentioned, but with more details.