52

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.

chuckj
  • 27,773
  • 7
  • 53
  • 49

6 Answers6

64

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
Matt
  • 3,617
  • 2
  • 27
  • 39
Naraen
  • 3,240
  • 2
  • 22
  • 20
26

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) {
    ...
});
Peter Cools
  • 276
  • 3
  • 3
  • 1
    This didn't work for me: The req 'error' event triggered with the following Error object, {"error":{"code":"ETIMEDOUT","errno":"ETIMEDOUT","syscall":"connect"}} on a request that without this augmentation did connect. – Drew Oct 01 '13 at 05:07
  • 1
    @Drew That typically means your firewall blocked the loopback connection. – EricLaw May 02 '14 at 16:18
  • @Drew. It could also be because your fiddler configuration is listening on a different port instead of (the default) 8888 – Naraen Nov 03 '14 at 20:05
  • Is this still up to date in October 2018? I get the same error @Drew got – Raketenolli Oct 17 '18 at 10:42
7

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 .

Pierre Arlaud
  • 4,040
  • 3
  • 28
  • 42
doron aviguy
  • 2,554
  • 2
  • 22
  • 18
  • Interestingly, I get [a similar error to the one @Drew mentions in his comment](https://stackoverflow.com/questions/8697344/can-a-proxy-like-fiddler-be-used-with-node-jss-clientrequest#comment28253465_10029125). `write EPROTO 101057795:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:openssl\ssl\s23_clnt.c:827` – Raketenolli Oct 17 '18 at 10:50
6
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";
Warlock
  • 7,321
  • 10
  • 55
  • 75
  • 1
    A browser will use fiddler as a proxy if it's running, but not if it's not running. The browser's request works in either case. How do I get the same behavior using node.js's request library? – John Deighan Jul 13 '17 at 08:06
  • I used the above in VSCode in a launch.json to debug node.js and capture the data in Fiddler: "env": { "https_proxy": "http://127.0.0.1:8888", "http_proxy": "http://127.0.0.1:8888", "NODE_TLS_REJECT_UNAUTHORIZED": "0" } – Ralph Hinkley Jul 17 '20 at 15:37
5

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.

Jarekczek
  • 7,456
  • 3
  • 46
  • 66
chuckj
  • 27,773
  • 7
  • 53
  • 49
2

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.

Tim Perry
  • 11,766
  • 1
  • 57
  • 85
  • 1
    I used the HTTP toolkit to help me debug a node app from Windows. It was easy and very helpful, I chose the option to Intercept\Fresh Terminal then just ran node .js. This made calls to push messages to AWS SQS via the "aws-sdk" library and the app intercepted requests perfectly – Rob Bowman Feb 01 '22 at 09:44