Use Fiddler with Node.js

Fiddler is an useful HTTP proxy debugger on Windows. It would be nice if it can work with Node.js applications. To do this, just need to proxy Node.js requests through Fiddler. The default proxy is This can be viewed in Fiddler Tools –> WInINET Options –> LAN settings –> Advanced:


Request module

If the request module is used to send requests, it is relatively easier. Environment variables can be used to turn on/off current node.js process’s fiddler proxy settings:

set https_proxy=
set http_proxy=
node Dixin.Nodejs\main.js

It’s done. After setting the above 3 environment variables, when calling request module to send requests, they will be captured by Fiddler. If the proxy settings needs to be turned off, just set these environment variables to empty:

set https_proxy=
set http_proxy=

This can be done with JavaScript. Here is part of a simple fiddler.js module:

"use strict";

var url = require("url"),
    http = require("http"),

    env = process.env,

    proxy = {
        protocol: "http:",
        hostname: "",
        port: 8888,

    proxyRequests = function () {
        var proxyUrl = url.format(proxy);
        env.http_proxy = proxyUrl;
        env.https_proxy = proxyUrl;

    unproxyRequests = function () {
        env.http_proxy = "";
        env.https_proxy = "";

The usage is straightforward:

var fiddler = require("./fiddler");
// requests.


If requests are sent with HTTP/HTTPS modules of Node.js, there is no global switch to turn on/off proxy settings for all requests. Some helper methods can be created to proxy an individual HTTP request to Fiddler. Below is the rest of the fiddler.js module:

    setProxy = function (options) {
        if (typeof options === "string") { // options can be URL string.
            options = url.parse(options);
        if (! && !options.hostname) {
            throw new Error("host or hostname must have value.");
        options.path = url.format(options);
        options.headers = options.headers || {};
        options.headers.Host = || url.format({
            hostname: options.hostname,
            port: options.port
        options.protocol = proxy.protocol;
        options.hostname = proxy.hostname;
        options.port = proxy.port;
        options.href = null; = null;
        return options;

    request = function (options, callback) {
        options = setProxy(options);
        return http.request(options, callback);
    get = function(options, callback) {
        options = setProxy(options);
        return http.get(options, callback);

module.exports = {
    proxy: proxy,
    proxyRequests: proxyRequests,
    unproxyRequests: unproxyRequests,
    setProxy: setProxy,
    request: request,
    get: get

When calling HTTP/HTTPS modules to send a request, call setProxy to proxy the request to Fiddler:

setProxy is used for the url/options, before passing it to call HTTP module methods, like request, get, etc. The following example proxies the request to URL string:

var http = require("http"),
    https = require("https"),
    fiddler = require("./fiddler");

var photoUrl = "";

https.request(photoUrl).end(); // Not through Fiddler.

http.request(fiddler.setProxy(photoUrl)).end(); // Through Fiddler.

The second request can be viewed in Fiddler:


Similarly, setProxy can be used for URI options:

var blogUrl = {
    protocol: "https:",
    hostname: "",
    pathname: "dixin"

https.get(blogUrl).end(); // Not through Fiddler.

http.get(fiddler.setProxy(blogUrl)); // Through Fiddler.

The second request is in Fiddler:


A get method and a request method are also provided. They are just shortcuts. The above calls are equivalent to:


The complete fiddler.js module is available in GitHub:


Add a Comment

As it will appear on the website

Not displayed

Your website