Javascript 代理(如 fiddler)可以与 Node.js 的 ClientRequest 一起使用吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8697344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can a proxy (like fiddler) be used with Node.js's ClientRequest
提问by chuckj
Can node.js be setup to recognize a proxy (like Fiddler for example) and route all ClientRequest's through the proxy?
可以将 node.js 设置为识别代理(例如 Fiddler)并通过代理路由所有 ClientRequest 吗?
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.
我在 Windows 上使用 node 并希望调试 http 请求,就像我在浏览器中使用 Fiddler for JavaScript 一样。
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.
请明确一点,我不是要创建代理,也不是要创建服务器收到的代理请求。我想路由http.request()
通过代理发出的请求。我想使用 Fiddler 来检查请求和响应,就像我在浏览器中执行请求一样。
采纳答案by Peter Cools
To route your client-requests via fiddler, alter your options-object like this (ex.: just before you create the http.request):
要通过 fiddler 路由您的客户端请求,请像这样更改您的选项对象(例如:就在创建 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) {
...
});
回答by Naraen
I find the following to be nifty. The requestmodule reads proxy information from the windows environment variable.
我发现以下内容很漂亮。所述请求模块读取从Windows环境变量代理信息。
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.
在 windows 命令提示符下键入以下内容,将在 shell 的生命周期内设置它。你只需要从这个 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
回答by doron aviguy
If you want to montior outgoing reqeusts from node you can use the requestmodule
如果您想监控来自节点的传出请求,您可以使用请求模块
and just set the proxyproperty 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 .
8888 是 fiddler 的默认端口。
回答by chuckj
Answering my own question: according to https://github.com/joyent/node/issues/1514the answer is no, but you can use the request
module, http://search.npmjs.org/#/request, which does support proxies.
回答我自己的问题:根据https://github.com/joyent/node/issues/1514答案是否定的,但您可以使用request
模块http://search.npmjs.org/#/request,它不会支持代理。
回答by Warlock
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";
回答by Tim Perry
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 intentionallyignores 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
但是,如果您只是在为 Node.js 寻找类似 fiddler 的 HTTP 调试工具,那么我一直在研究一个名为HTTP Toolkit的开源项目(具有内置节点支持)。它让你
- 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.
- 一键从应用程序打开终端
- 从该终端启动任何节点 CLI/服务器/脚本
- 它发送的所有 HTTP 或 HTTPS 请求都会自动代理,因此您可以查看和重写所有内容。无需更改代码或 npm 包。
Here's a demo of it debugging a bunch of NPM, node & browser traffic:
这是它调试一堆 NPM、节点和浏览器流量的演示:
Internally, the way this works is that it injects an extra JS scriptinto started Node processes, which hooks into require()
to automatically reconfigure proxy settings for you, for every module which doesn't use the global settings.
在内部,它的工作方式是将一个额外的JS 脚本注入到启动的 Node 进程中,该脚本会require()
为每个不使用全局设置的模块自动重新配置代理设置。