Use node-XMLHttpRequest through a Proxy?

喜夏-厌秋 提交于 2019-12-18 09:39:11

问题


I need to deploy my node-XMLHttpRequest app to a server that uses a proxy to access the internet. In a terminal on that server, I can curl -d "" http://website/path and it works fine. I think curl recognizes the environment variable http_proxy in that case. The node app times out because it doesn't see the proxy. How can I get the node app to use the proxy?

For instance, could I use http.globalAgent to send the requests through the proxy?


回答1:


Did you provide the proxy options to the http connection?

The below should help.

var http = require("http");

var options = {
hostname: "<proxy-server-address>",
port: <proxy-server-port>,
path: "http://www.xyz.com",
headers: {
  Host: "www.xyz.com"
         }
};

var req = http.request(options, function(res){ ... });

req.end();



回答2:


This fork adds a config object that can specify a proxy to use.

https://github.com/jbeuckm/node-XMLHttpRequest



来源:https://stackoverflow.com/questions/22844839/use-node-xmlhttprequest-through-a-proxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!