Can a proxy (like fiddler) be used with Node.js's ClientRequest

Javascriptnode.jsFiddler

Javascript Problem Overview


Can node.js be setup to recognize a proxy (like Fiddler for example) and route all ClientRequest's through the proxy?

I am using node on Windows and would like to debug the http requests much like I would using Fiddler for JavaScript in the browser.

Just be clear, I am not trying to create a proxy nor proxy requests received by a server. I want to route requests made by http.request() through a proxy. I would like to use Fiddler to inspect both the request and the response as I would if I was performing the request in a browser.

Javascript Solutions


Solution 1 - Javascript

I find the following to be nifty. The request module reads proxy information from the windows environment variable.

Typing the following in the windows command prompt, will set it for the lifetime of the shell. You just have to run your node app from this shell.

set https_proxy=http://127.0.0.1:8888 
set http_proxy=http://127.0.0.1:8888
set NODE_TLS_REJECT_UNAUTHORIZED=0

Solution 2 - Javascript

To route your client-requests via fiddler, alter your options-object like this (ex.: just before you create the http.request):

options.path = 'http://' + options.host + ':' + options.port + options.path;
options.headers.host = options.host;
options.host = '127.0.0.1';
options.port = 8888;

myReq = http.request(options, function (result) {
    ...
});

Solution 3 - Javascript

If you want to montior outgoing reqeusts from node you can use the request module

and just set the proxy property in the options, like that

request.post('http://204.145.74.56:3003/test', {
headers :{ 'content-type' : 'application/octet-stream'}, 
'body' : buf ,
 proxy: 'http://127.0.0.1:8888'
}, function() {
   //callback
});

8888 is the default port , of fiddler .

Solution 4 - Javascript

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";

Solution 5 - Javascript

Answering my own question: according to https://github.com/joyent/node/issues/1514 the answer is no, but you can use the request module, http://search.npmjs.org/#/request, which does support proxies.

Solution 6 - Javascript

If you want to configure a proxy in the general case, the other answers are right: you need to manually configure that for the library you're using as node intentionally ignores your system proxy settings out of the box.

If however you're simply looking for a fiddler-like HTTP debugging tool for Node.js, I've been working on an open-source project to do this for a little while (with built-in node support) called HTTP Toolkit. It lets you

  • Open a terminal from the app with one click
  • Start any node CLI/server/script from that terminal
  • All the HTTP or HTTPS requests it sends get proxied automatically, so you can see and rewrite everything. No code changes or npm packages necessary.

Here's a demo of it debugging a bunch of NPM, node & browser traffic:

Demo screenshot

Internally, the way this works is that it injects an extra JS script into started Node processes, which hooks into require() to automatically reconfigure proxy settings for you, for every module which doesn't use the global settings.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionchuckjView Question on Stackoverflow
Solution 1 - JavascriptNaraenView Answer on Stackoverflow
Solution 2 - JavascriptPeter CoolsView Answer on Stackoverflow
Solution 3 - Javascriptdoron aviguyView Answer on Stackoverflow
Solution 4 - JavascriptWarlockView Answer on Stackoverflow
Solution 5 - JavascriptchuckjView Answer on Stackoverflow
Solution 6 - JavascriptTim PerryView Answer on Stackoverflow