javascriptnode.jsexpressfiddlerproxy-server

Express Proxy Server, to Proxy Fiddler


I have a node.js express proxy server, which I would like to debug, So I want to capture the traffic through fiddler:

const express = require('express');
const expressApp = express();
const proxyTarget = 'https://my-proxy.azurewebsites.net/proxy';

//attempting to configure fiddler proxy 
process.env.https_proxy= 'http://127.0.0.1:8888'
process.env.http_proxy= 'http://127.0.0.1:8888'
process.env.NODE_TLS_REJECT_UNAUTHORIZED=0;

expressApp.use('/api', proxy({
    target: proxyTarget,
    secure: false, // don't verify https certificates
    logLevel: 'debug'
}));

I've tried to capture traffic using fiddler but fiddler doesn't capture the request to Azure, only the wrapped proxy request on localhost.

How can I configure my proxy settings so that first I pass through fiddler?


Solution

  • Unfortunately, Node.js doesn't listen to the http_proxy/https_proxy environment variables (see https://github.com/nodejs/node/issues/8381 for the full debate).

    That means this is difficult with Fiddler - you need to change every place where HTTP requests are made to use the proxy settings, or to replace Node's default agent to do so (and then ensure no requests set a custom agent). There are some node modules that can help here, like https://www.npmjs.com/package/global-agent, but you'll also need to handle HTTPS trust separately (or you'll just see unreadable CONNECT requests, as mentioned by the commenter above).

    I have hit this same issue myself though, so I've been building an open-source alternative to Fiddler to fix it! HTTP Toolkit can do what you're looking for: you can open a terminal from there (or enable it in an existing terminal) and any node processes started there will automatically have all HTTP & HTTPS requests intercepted, so you can examine and/or rewrite them, just like Fiddler. It handles all the cert trust stuff too.

    Under the hood, this is just wrapping node on the command line to do the same thing you'd do manually to do that, reconfiguring settings & defaults to ensure everything plays nicely. If you do want to do that manually, the full source to do so is here: https://github.com/httptoolkit/httptoolkit-server/blob/master/overrides/js/prepend-node.js