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 127.0.0.1:8888. This can be viewed in Fiddler Tools –> WInINET Options –> LAN settings –> Advanced:

image

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=http://127.0.0.1:8888
set http_proxy=http://127.0.0.1:8888
set NODE_TLS_REJECT_UNAUTHORIZED=0
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=
set NODE_TLS_REJECT_UNAUTHORIZED=

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: "127.0.0.1",
        port: 8888,
    },

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

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

The usage is straightforward:

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

HTTP/HTTPS module

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.host && !options.hostname) {
            throw new Error("host or hostname must have value.");
        }
        options.path = url.format(options);
        options.headers = options.headers || {};
        options.headers.Host = options.host || url.format({
            hostname: options.hostname,
            port: options.port
        });
        options.protocol = proxy.protocol;
        options.hostname = proxy.hostname;
        options.port = proxy.port;
        options.href = null;
        options.host = 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://c1.staticflickr.com/3/2875/9215169916_f8fa57c3da_b.jpg";

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

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

The second request can be viewed in Fiddler:

image

Similarly, setProxy can be used for URI options:

var blogUrl = {
    protocol: "https:",
    hostname: "weblogs.asp.net",
    pathname: "dixin"
};

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

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

The second request is in Fiddler:

image

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

fiddler.request(photoUrl).end();
fiddler.get(blogUrl);

The complete fiddler.js module is available in GitHub: https://github.com/Dixin/CodeSnippets/blob/master/Dixin.Nodejs/fiddler.js.

26 Comments

  • Great article!

  • Thank you very much for this post, very helpful.

  • Thank you very much for this post, very helpful.

  • awesome!!

  • Thanks. really useful.

  • Thanks - very useful! just an observation that the link to the complete code is 404ing now.

  • @Mike - the url has changed to: https://github.com/Dixin/CodeSnippets/blob/master/Blog/Dixin.Nodejs/fiddler.js

  • Solved my problems. Thank you :)

  • شركت پارس اتیلن کیش بواسطه تولید محصولات مرغوب و با کیفیت در اکثر پروژه های ملی همکاری داشته كه اين همكاري در زمينه تامين لوله، اتصالات و منهول پلي اتيلن براي اين پروژه ها بوده است.
    https://www.parsethylene.com/

  • After searching through many articles on Internet your article helped me to resolve my issue.
    I was trying to run APIs with artillery through the command prompt. The issue got resolved. thanks

  • https://ma-study.blogspot.com/

  • Spotify apk is the best app for listening music

  • When I read an article on this topic, <a href="http://maps.google.de/url?sa=t&url=https%3A%2F%2Foncasino.io">casinosite</a> the first thought was profound and difficult, and I wondered if others could understand.. My site has a discussion board for articles and photos similar to this topic. Could you please visit me when you have time to discuss this topic?
    ...

  • i like your article I look forward to receiving new news from you every day. Your content is complete. I really like it. Thank you.

  • Such an informative article. Thanks for sharing.

  • I am very impressed with your writing I couldn't think of this, but it's amazing! I wrote several posts similar to this one, but please come and see!

  • However, some fiddler players believe in lucky timings, like playing when the casino isn't too crowded node.js or trying a slot that hasn't paid out big in a while. But remember, it's all part of the excitement, and there are no guarantees.


  • برای تهیه میلگرد بستر مراحل طولانی سپری می شود .مراحلی چون تهیه مواد خام برای میلگرد بستر. کشش میلگرد بستر . برش میلگرد بستر و مراحل خم کاری و جوش میلگرد بستر و در آخر آب کاری میلگرد بستر تمامی این هزینه ها در قیمت میلگرد بستر موثر است.و تمامی قیمت های میلگرد بستر به این عوامل ارتباط مستقیم دارند .تلاش ما این است با استفاده از تیم مجرب با کم ترین هزینه و بهترین کیفیت میلگرد بستر را عرضه و تولید کنیم.
    <a href="https://milgerdbastarmph.com/">قیمت میلگرد بستر</a>


  • تولید کننده انواع سازه های فلزی در ایران با بهترین قیمت و کیفیت

    <a href="https://amodfoladparsian.ir/"> شرکت ساخت سازه های فلزی</a>


  • چیپس و پفک‌ها از نوعی تنقلات هستند که در سال‌های اخیر به شدت محبوبیت پیدا کرده‌اند. این محصولات به دلیل طعم و رنگ جذاب، قابلیت حمل و نقل آسان، و توانایی برطرف کردن گرسنگی به صورت سریع، در بسیاری از کشورها پرمصرف هستند. البته باید توجه داشت که مصرف بیش از حد چیپس و پفک‌ها ممکن است باعث افزایش وزن و بهبود نادرست رفتاری در کودکان شود. چیپس‌ها از قطعات برش‌شده سیب‌زمینی تهیه می‌شوند که بعد از فرایند تنقیه، سرخ شدن و اضافه کردن نمک یا ادویه‌های مختلف به عنوان یک محصول خشک شده در بسته‌بندی‌های مختلفی به بازار عرضه می‌شوند. اما در طی فرایند تولید چیپس، ممکن است مواد شیمیایی نظیر روغن‌های نامناسب و رنگ‌های صنعتی به آنها اضافه شود که این مواد باعث افزایش کالری و کاهش ارزش غذایی چیپس می‌شوند. پفک‌ها نیز از طریق فرایندی شبیه به تولید چیپس، با استفاده از جو، ذرت و سایر مواد غذایی تهیه می‌شوند. پفک‌ها ممکن است شامل مقادیر بالایی از نمک و چربی باشند که باعث افزایش کالری و کاهش ارزش غذایی آنها می‌شود. بهترین راه برای مصرف چیپس و پفک، محدود کردن مصرف آنها به میان وعده‌ها و جایگزینی آنها با تنقلات سالم‌تری مانند میوه‌های خشک شده، آجیل و سایر انواع تنقلات سالم است. همچنین باید به دنبال بسته‌بندی‌هایی باشیم که حاوی مواد شیمیایی نباشند و اطمینان حاصل کنیم که این محصولات با استانداردهای بهداشتی سازگار هستند. در نهایت، تنقلات چیپس و پفک‌ها با وجود طعم و رنگ جذاب، به دلیل داشتن کالری بالا و مواد شیمیایی آلوده، بهتر است که به میزان محدود مصرف شوند و از تنقلات سالم‌تری برای تأمین نیازهای میان وعده‌های خود استفاده کنیم. پخش عمده تنقلات در بازرگانی مهر فراز با بهترین قیمت
    <a href="https://mehrfaraz.com/dtailprod/2/"> پخش عمده چیپس پفک</a>

  • Of course, your article is good enough, <a href="https://toolbarqueries.google.tk/url?sa=t&url=https%3A%2F%2Fwww.mtclean.blog/">totosite</a> but I thought it would be much better to see professional photos and videos together. There are articles and photos on these topics on my homepage, so please visit and share your opinions.

  • When I read an article on this topic, <a href="https://toolbarqueries.google.tg/url?sa=t&url=https%3A%2F%2Fwww.mtclean.blog/">baccarat online</a> the first thought was profound and difficult, and I wondered if others could understand.. My site has a discussion board for articles and photos similar to this topic. Could you please visit me when you have time to discuss this topic?

  • Your writing is perfect and complete. <a href="https://toolbarqueries.google.td/url?sa=t&url=https%3A%2F%2Fwww.mtclean.blog/">casinosite</a> However, I think it will be more wonderful if your post includes additional topics that I am thinking of. I have a lot of posts on my site similar to your topic. Would you like to visit once?

  • As I am looking at your writing, <a href="https://toolbarqueries.google.st/url?sa=t&url=https%3A%2F%2Fwww.mtclean.blog/">baccaratsite</a> I regret being unable to do outdoor activities due to Corona 19, and I miss my old daily life. If you also miss the daily life of those days, would you please visit my site once? My site is a site where I post about photos and daily life when I was free.

  • WordPress speed optimization is the process of improving the performance and loading speed of a WordPress website. A fast-loading website is crucial for providing a good user experience, improving search engine rankings, and increasing conversions. Here are some tips and techniques for optimizing the speed of a WordPress site:

    1. Choose a Fast Web Hosting Provider: Selecting a reliable and high-performance web hosting provider is essential. Look for providers that offer optimized WordPress hosting or managed WordPress hosting plans that are specifically designed to enhance speed and performance.

    2. Use a Lightweight and Fast Theme: Opt for a lightweight and well-coded WordPress theme. Avoid using bloated themes with excessive features and functionality that you don't need. Lightweight themes typically load faster and provide a better foundation for optimization.

    3. Utilize Caching: Implement a caching mechanism to store static versions of your web pages and serve them to visitors, reducing server load and improving response times. WordPress plugins like WP Super Cache and W3 Total Cache can help with this.

    4. Optimize Images: Large image files can significantly slow down your website. Compress and optimize images before uploading them to your WordPress site. You can use plugins like Smush or EWWW Image Optimizer to automatically optimize your images.

    5. Minify and Combine CSS and JavaScript: Minify your CSS and JavaScript files to remove unnecessary characters, comments, and whitespace. Additionally, combine multiple CSS and JavaScript files into a single file to reduce the number of HTTP requests made by the browser.

    6. Enable GZIP Compression: Enable GZIP compression on your web server to compress your website's files before they are sent to the visitor's browser. This reduces the file size and speeds up page loading.

    7. Use a Content Delivery Network (CDN): A CDN stores copies of your website's static files (CSS, JavaScript, images) on servers located worldwide. When a user requests your website, the CDN serves the files from the server closest to their location, reducing latency and improving load times.

    8. Optimize WordPress Database: Regularly optimize and clean up your WordPress database to remove unnecessary data, spam comments, post revisions, and transients. Plugins like WP-Optimize or WP-Sweep can help with this task.

    9. Limit and Monitor Plugins: Use only essential plugins and regularly review and deactivate any unnecessary plugins. Too many plugins, especially poorly coded ones, can slow down your site. Regularly monitor the performance impact of your active plugins using tools like Query Monitor or P3 Plugin Performance Profiler.

    10. Enable Lazy Loading: Implement lazy loading for images and videos, which means that only the content visible on the screen is loaded initially, while the rest is loaded as the user scrolls down the page. This reduces the initial page load time.

    11. Implement AMP (Accelerated Mobile Pages): Implementing AMP for your WordPress site creates stripped-down versions of your pages that load quickly on mobile devices. AMP can significantly improve mobile performance and user experience.

    12. Keep WordPress and Plugins Updated: Regularly update your WordPress core, themes, and plugins to ensure you have the latest versions with bug fixes and performance improvements.

    Remember to always take a backup of your website before making any significant changes, and test the performance improvements after each optimization to ensure they have the desired effect.

  • Thanks. really useful.

Add a Comment

As it will appear on the website

Not displayed

Your website