Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

node.js - Use node-XMLHttpRequest through a Proxy?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...