Capture NodeJS Traffic with Charles Proxy

Inspecting traffic is very valuable for debugging and troubleshooting. Let’s assume that your application calls to myapi.com and receives unexpected data. Want to view actual request and/or response headers and body? Charles proxy can receive traffic from the application and send it to the target server: myapi.com while capturing all information.

Quick and simple approach described below will let capture your NodeJS application traffic: both HTTP and SSL / HTTPS. No additional dependencies required: only Charles proxy.

Configuring Charles:
  1. Go to Proxy » Reverse Proxies…
  2. Check Enable Reverse Proxies, add a target server:
    Local Port: 60103 - any unused local port
    Remote Host: myapi.com - target host address
    Remote Port: 443 - target host port
  • For SSL / HTTPS traffic:
  1. Go to Proxy » SSL Proxying Settings… » SSL Proxying tab
  2. Check Enable SSL Proxying, add a target server:
    Host: myapi.com
    Port: 443
Configuring NodeJS application:
  1. Change requests options to point to Charles proxy instead of myapi.com:
    host: localhost
    port: 60103 - local port from reverse proxy settings
  2. Charles uses self-generated SSL certificates, so to prevent certificate errors add following line:
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

Done! Enjoy debugging.

You could apply the rules only for debugging or development environment, for example with environment variable.

var HTTPS = require('https');

// Default options
var options = {
    host: 'myapi.com',
    path: '/',
    method: 'POST',
    protocol: 'https:',
    port: '443'
};

// Edit options if debugging
if (process.env.NODE_ENV === "development") {
    process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
    options.host = 'localhost';
    options.port = '60103';
}

var request = HTTPS.request(options, function(res) {
    /* process */
});

Charles proxy version used: v 3.10.1. It was tested on Mac OS and should work on Linux and Windows as well. You could also use free Fiddler proxy on PC.

comments powered by Disqus