Inspecting node.js traffic with Charles and tunnel
If you’ve ever had to debug network traffic, you know the value of a good tool like Fiddler or Charles. There are some immensely useful features, like the ability to edit and replay requests, but you can also get a lot of value just from inspecting network traffic.
Charles will automagically set itself up as a proxy for browser traffic, but things can get a little trickier when you want to intercept http requests in node.js.
This is where tunnel comes in. Tunnel let’s you tunnel traffic through a proxy with a simple tunneling agent.
When you set up your http request, you’ll usually specify a host and a port:
var http = require('http');
var req = http.request({
host: 'example.com',
port: 80
});
With tunnel, you can specify a tunneling proxy agent like so:
var http = require('http'),
tunnel = require('tunnel');
var tunnelingAgent = tunnel.httpOverHttp({
proxy: {
host: 'localhost',
port: 8888 // default Charles proxy port
}
});
var req = http.request({
host: 'example.com',
port: 80,
agent: tunnelingAgent
});
Viola! Load up Charles and watch the magic happen.
Tunnel has some pretty sweet ssl support as well.