I want to make an outgoing HTTP call from node.js, using the standard http.Client
. But I cannot reach the remote server directly from my network and need to go through a proxy.
我想从node发出一个HTTP调用。使用标准http.Client。但是我不能直接从我的网络到达远程服务器,需要通过一个代理。
How do I tell node.js to use the proxy?
如何告诉节点。js使用代理吗?
16 个解决方案
#1
125
Tim Macfarlane's answer was close with regards to using a HTTP proxy.
关于使用HTTP代理,Tim Macfarlane的回答非常接近。
Using a HTTP proxy (for non secure requests) is very simple. You connect to the proxy and make the request normally except that the path part includes the full url and the host header is set to the host you want to connect to.
Tim was very close with his answer but he missed setting the host header properly.
使用HTTP代理(针对非安全请求)非常简单。您连接到代理并发出请求,除非路径部分包含完整的url,并且主机头被设置为要连接的主机。蒂姆非常接近他的答案,但是他没有正确地设置主队的头球。
var http = require("http");
var options = {
host: "proxy",
port: 8080,
path: "http://www.google.com",
headers: {
Host: "www.google.com"
}
};
http.get(options, function(res) {
console.log(res);
res.pipe(process.stdout);
});
For the record his answer does work with http://nodejs.org/ but that's because their server doesn't care the host header is incorrect.
对于记录,他的答案确实与http://nodejs.org/有关,但这是因为他们的服务器并不关心主机标题的错误。
#2
42
You can use request, I just found it's unbelievably easy to use proxy on node.js, just with one external "proxy" parameter, even more it supports HTTPS through a http proxy.
你可以使用request,我刚发现在节点上使用proxy非常简单。js,只有一个外部的“代理”参数,更多的是通过http代理支持HTTPS。
var request = require('request');
request({'url':'https://anysite.you.want/sub/sub',
'proxy':'http://yourproxy:8087'}, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
})
#3
29
One thing that took me a while to figure out, use 'http' to access the proxy, even if you're trying to proxy through to a https server. This works for me using Charles (osx protocol analyser):
有一件事我花了一段时间才弄明白,使用“http”来访问代理,即使您试图代理到https服务器。这对于我使用Charles (osx协议分析器):
var http = require('http');
http.get ({
host: '127.0.0.1',
port: 8888,
path: 'https://www.google.com/accounts/OAuthGetRequestToken'
}, function (response) {
console.log (response);
});
#4
14
As @Renat here already mentioned, proxied HTTP traffic comes in pretty normal HTTP requests. Make the request against the proxy, passing the full URL of the destination as the path.
正如这里提到的@Renat, proxied HTTP流量是正常的HTTP请求。对代理发出请求,将目标的完整URL作为路径传递。
var http = require ('http');
http.get ({
host: 'my.proxy.com',
port: 8080,
path: 'http://nodejs.org/'
}, function (response) {
console.log (response);
});
#5
8
The 'request' http package seems to have this feature:
“请求”http包似乎具有以下特性:
https://github.com/mikeal/request
https://github.com/mikeal/request
For example, the 'r' request object below uses localproxy to access its requests:
例如,下面的“r”请求对象使用localproxy访问其请求:
var r = request.defaults({'proxy':'http://localproxy.com'})
http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
r.get('http://google.com/doodle.png').pipe(resp)
}
})
Unfortunately there are no "global" defaults so that users of libs that use this cannot amend the proxy unless the lib pass through http options...
不幸的是,没有“全局”的默认值,因此使用这种方法的libs用户不能修改代理,除非lib通过http选项……
HTH, Chris
HTH,克里斯
#6
8
Thought I would add this module I found: https://www.npmjs.org/package/global-tunnel, which worked great for me (Worked immediately with all my code and third party modules with only the code below).
我想我应该添加我发现的这个模块:https://www.npmjs.org/package/global-tunnel,它对我来说非常有用(我的所有代码和第三方模块立即起作用,只有下面的代码)。
require('global-tunnel').initialize({
host: '10.0.0.10',
port: 8080
});
Do this once, and all http (and https) in your application goes through the proxy.
这样做一次,应用程序中的所有http(和https)都将通过代理。
Alternately, calling
此外,调用
require('global-tunnel').initialize();
Will use the http_proxy
environment variable
将使用http_proxy环境变量吗
#7
5
Basically you don't need an explicit proxy support. Proxy protocol is pretty simple and based on the normal HTTP protocol. You just need to use your proxy host and port when connecting with HTTPClient. Example (from node.js docs):
基本上,您不需要显式的代理支持。代理协议非常简单,并且基于正常的HTTP协议。您只需要在连接HTTPClient时使用代理主机和端口。例子(从节点。js文件):
var http = require('http');
var google = http.createClient(3128, 'your.proxy.host');
var request = google.request('GET', '/',
{'host': 'www.google.com'});
request.end();
...
So basically you connect to your proxy but do a request to "http://www.google.com".
基本上你连接到你的代理,但是请求到http://www.google.com。
#8
4
In case you need to the use basic authorisation for your proxy provider, just use the following:
如果您需要为您的代理提供商使用基本授权,请使用以下内容:
var http = require("http");
var options = {
host: FarmerAdapter.PROXY_HOST,
port: FarmerAdapter.PROXY_PORT,
path: requestedUrl,
headers: {
'Proxy-Authorization': 'Basic ' + new Buffer(FarmerAdapter.PROXY_USER + ':' + FarmerAdapter.PROXY_PASS).toString('base64')
}
};
var request = http.request(options, function(response) {
var chunks = [];
response.on('data', function(chunk) {
chunks.push(chunk);
});
response.on('end', function() {
console.log('Response', Buffer.concat(chunks).toString());
});
});
request.on('error', function(error) {
console.log(error.message);
});
request.end();
#9
3
Node should support using the http_proxy environmental variable - so it is cross platform and works on system settings rather than requiring a per-application configuration.
节点应该支持使用http_proxy环境变量——因此它是跨平台的,可以在系统设置上工作,而不需要每个应用程序配置。
Using the provided solutions, I would recommend the following:
使用所提供的解决方案,我建议如下:
Coffeescript
Coffeescript
get_url = (url, response) ->
if process.env.http_proxy?
match = process.env.http_proxy.match /^(http:\/\/)?([^:\/]+)(:([0-9]+))?/i
if match
http.get { host: match[2], port: (if match[4]? then match[4] else 80), path: url }, response
return
http.get url, response
Javascript
Javascript
get_url = function(url, response) {
var match;
if (process.env.http_proxy != null) {
match = process.env.http_proxy.match(/^(http:\/\/)?([^:\/]+)(:([0-9]+))?/i);
if (match) {
http.get({
host: match[2],
port: (match[4] != null ? match[4] : 80),
path: url
}, response);
return;
}
}
return http.get(url, response);
};
Usage To use the method, effectively just replace http.get, for instance the following writes the index page of google to a file called test.htm:
使用该方法,只需有效地替换http。例如,下面将谷歌的索引页写到一个名为test.htm的文件:
file = fs.createWriteStream path.resolve(__dirname, "test.htm")
get_url "http://www.google.com.au/", (response) ->
response.pipe file
response.on "end", ->
console.log "complete"
#10
1
Imskull's answer almost worked for me, but I had to make some changes. The only real change is adding username, password, and setting rejectUnauthorized to false. I couldn't comment so I put this in an answer.
Imskull的回答几乎对我起了作用,但是我必须做出一些改变。唯一真正的改变是添加用户名、密码和设置rejectUnauthorized为false。我无法评论,所以我把它放在了一个答案里。
If you run the code it'll get you the titles of the current stories on Hacker News, per this tutorial: http://smalljs.org/package-managers/npm/
如果您运行代码,它将为您提供关于Hacker News的当前故事的标题,参见本教程:http://smalljs.org/package-managers/npm/
var cheerio = require('cheerio');
var request = require('request');
request({
'url': 'https://news.ycombinator.com/',
'proxy': 'http://Username:Password@YourProxy:Port/',
'rejectUnauthorized': false
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
if (response.body) {
var $ = cheerio.load(response.body);
$('td.title a').each(function() {
console.log($(this).text());
});
}
} else {
console.log('Error or status not equal 200.');
}
});
#11
1
I bought private proxy server, after purchase I got:
我购买了私人代理服务器,购买后我得到:
255.255.255.255 // IP address of proxy server
99999 // port of proxy server
username // authentication username of proxy server
password // authentication password of proxy server
And I wanted to use it. First answer and second answer worked only for http(proxy) -> http(destination), however I wanted http(proxy) -> https(destination).
我想用它。第一个答案和第二个答案只适用于http(代理)-> http(目的地),但是我想要http(代理)-> https(目的地)。
And for https destination it would be better to use HTTP tunnel directly. I found solution here. Final code:
对于https目的地,最好直接使用HTTP隧道。我发现解决方案。最后的代码:
const http = require('http')
const https = require('https')
const username = 'username'
const password = 'password'
const auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64')
http.request({
host: '255.255.255.255', // IP address of proxy server
port: 99999, // port of proxy server
method: 'CONNECT',
path: 'kinopoisk.ru:443', // some destination, add 443 port for https!
headers: {
'Proxy-Authorization': auth
},
}).on('connect', (res, socket) => {
if (res.statusCode === 200) { // connected to proxy server
https.get({
host: 'www.kinopoisk.ru',
socket: socket, // using a tunnel
agent: false // cannot use a default agent
}, (res) => {
let chunks = []
res.on('data', chunk => chunks.push(chunk))
res.on('end', () => {
console.log('DONE', Buffer.concat(chunks).toString('utf8'))
})
})
}
}).on('error', (err) => {
console.error('error', err)
}).end()
#12
0
May not be the exact one-liner you were hoping for but you could have a look at http://github.com/nodejitsu/node-http-proxy as that may shed some light on how you can use your app with http.Client.
可能不是您所期望的那种一行程序,但您可以查看http://github.com/nodejitsu/node-http-proxy,因为这可能有助于了解如何在http.Client中使用应用程序。
#13
0
http://groups.google.com/group/nodejs/browse_thread/thread/d5aadbcaa00c3f7/12ebf01d7ec415c3?lnk=gst&q=proxy 12 ebf01d7ec415c3
Based on the answers from this thread it would seem like you could use proxychains to run node.js through the proxy server:$ proxychains /path/to/node application.js
根据这个线程的回答,似乎可以使用proxychain来运行node。通过代理服务器的js: $ proxychain /path/to/node application.js
Personally I wasnt able to install any of the proxychains versions on Cygwin/Windows environment so couldn't test it.
我个人无法在Cygwin/Windows环境中安装任何proxychain版本,因此无法对其进行测试。
Furthermore, they also talked about using connect-proxy but I could not find any documentation on how to do this.
此外,他们还讨论了使用connect-proxy,但我找不到任何关于如何使用它的文档。
In short, I'm still stuck, but maybe someone can use this info to find a suitable work-around.
简而言之,我还是被困住了,但是也许有人可以利用这个信息找到合适的工作。
#14
0
For using a proxy with https I tried the advice on this website (using dependency https-proxy-agent) and it worked for me:
对于使用https代理,我尝试了这个网站上的建议(使用依赖关系httpsproxy -agent),它对我起了作用:
http://codingmiles.com/node-js-making-https-request-via-proxy/
http://codingmiles.com/node-js-making-https-request-via-proxy/
#15
0
use 'https-proxy-agent' like this
使用“https-proxy-agent”这样的
var HttpsProxyAgent = require('https-proxy-agent');
var proxy = process.env.https_proxy || 'other proxy address';
var agent = new HttpsProxyAgent(proxy);
options = {
//...
agent : agent
}
https.get(options, (res)=>{...});
#16
0
Here just a simple sample if you don't want to use crypto or Buffer. You could make base64 encode of -> myuser:mypassword, and then add "Basic " in the beginning. That's the value of "Proxy-Authorization" header.
这里只是一个简单的示例,如果您不想使用加密或缓冲区的话。您可以将base64编码为-> myuser:mypassword,然后在开头添加“Basic”。这是“代理授权”头的值。
var Http = require('http');
var req = Http.request({
host: 'myproxy.com.zx',
port: 8080,
headers:{"Proxy-Authorization": "Basic bXl1c2VyOm15cGFzcw"},
method: 'GET',
path: 'http://www.google.com/'
}, function (res) {
res.on('data', function (data) {
console.log(data.toString());
});
});
req.end();
#1
125
Tim Macfarlane's answer was close with regards to using a HTTP proxy.
关于使用HTTP代理,Tim Macfarlane的回答非常接近。
Using a HTTP proxy (for non secure requests) is very simple. You connect to the proxy and make the request normally except that the path part includes the full url and the host header is set to the host you want to connect to.
Tim was very close with his answer but he missed setting the host header properly.
使用HTTP代理(针对非安全请求)非常简单。您连接到代理并发出请求,除非路径部分包含完整的url,并且主机头被设置为要连接的主机。蒂姆非常接近他的答案,但是他没有正确地设置主队的头球。
var http = require("http");
var options = {
host: "proxy",
port: 8080,
path: "http://www.google.com",
headers: {
Host: "www.google.com"
}
};
http.get(options, function(res) {
console.log(res);
res.pipe(process.stdout);
});
For the record his answer does work with http://nodejs.org/ but that's because their server doesn't care the host header is incorrect.
对于记录,他的答案确实与http://nodejs.org/有关,但这是因为他们的服务器并不关心主机标题的错误。
#2
42
You can use request, I just found it's unbelievably easy to use proxy on node.js, just with one external "proxy" parameter, even more it supports HTTPS through a http proxy.
你可以使用request,我刚发现在节点上使用proxy非常简单。js,只有一个外部的“代理”参数,更多的是通过http代理支持HTTPS。
var request = require('request');
request({'url':'https://anysite.you.want/sub/sub',
'proxy':'http://yourproxy:8087'}, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
})
#3
29
One thing that took me a while to figure out, use 'http' to access the proxy, even if you're trying to proxy through to a https server. This works for me using Charles (osx protocol analyser):
有一件事我花了一段时间才弄明白,使用“http”来访问代理,即使您试图代理到https服务器。这对于我使用Charles (osx协议分析器):
var http = require('http');
http.get ({
host: '127.0.0.1',
port: 8888,
path: 'https://www.google.com/accounts/OAuthGetRequestToken'
}, function (response) {
console.log (response);
});
#4
14
As @Renat here already mentioned, proxied HTTP traffic comes in pretty normal HTTP requests. Make the request against the proxy, passing the full URL of the destination as the path.
正如这里提到的@Renat, proxied HTTP流量是正常的HTTP请求。对代理发出请求,将目标的完整URL作为路径传递。
var http = require ('http');
http.get ({
host: 'my.proxy.com',
port: 8080,
path: 'http://nodejs.org/'
}, function (response) {
console.log (response);
});
#5
8
The 'request' http package seems to have this feature:
“请求”http包似乎具有以下特性:
https://github.com/mikeal/request
https://github.com/mikeal/request
For example, the 'r' request object below uses localproxy to access its requests:
例如,下面的“r”请求对象使用localproxy访问其请求:
var r = request.defaults({'proxy':'http://localproxy.com'})
http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
r.get('http://google.com/doodle.png').pipe(resp)
}
})
Unfortunately there are no "global" defaults so that users of libs that use this cannot amend the proxy unless the lib pass through http options...
不幸的是,没有“全局”的默认值,因此使用这种方法的libs用户不能修改代理,除非lib通过http选项……
HTH, Chris
HTH,克里斯
#6
8
Thought I would add this module I found: https://www.npmjs.org/package/global-tunnel, which worked great for me (Worked immediately with all my code and third party modules with only the code below).
我想我应该添加我发现的这个模块:https://www.npmjs.org/package/global-tunnel,它对我来说非常有用(我的所有代码和第三方模块立即起作用,只有下面的代码)。
require('global-tunnel').initialize({
host: '10.0.0.10',
port: 8080
});
Do this once, and all http (and https) in your application goes through the proxy.
这样做一次,应用程序中的所有http(和https)都将通过代理。
Alternately, calling
此外,调用
require('global-tunnel').initialize();
Will use the http_proxy
environment variable
将使用http_proxy环境变量吗
#7
5
Basically you don't need an explicit proxy support. Proxy protocol is pretty simple and based on the normal HTTP protocol. You just need to use your proxy host and port when connecting with HTTPClient. Example (from node.js docs):
基本上,您不需要显式的代理支持。代理协议非常简单,并且基于正常的HTTP协议。您只需要在连接HTTPClient时使用代理主机和端口。例子(从节点。js文件):
var http = require('http');
var google = http.createClient(3128, 'your.proxy.host');
var request = google.request('GET', '/',
{'host': 'www.google.com'});
request.end();
...
So basically you connect to your proxy but do a request to "http://www.google.com".
基本上你连接到你的代理,但是请求到http://www.google.com。
#8
4
In case you need to the use basic authorisation for your proxy provider, just use the following:
如果您需要为您的代理提供商使用基本授权,请使用以下内容:
var http = require("http");
var options = {
host: FarmerAdapter.PROXY_HOST,
port: FarmerAdapter.PROXY_PORT,
path: requestedUrl,
headers: {
'Proxy-Authorization': 'Basic ' + new Buffer(FarmerAdapter.PROXY_USER + ':' + FarmerAdapter.PROXY_PASS).toString('base64')
}
};
var request = http.request(options, function(response) {
var chunks = [];
response.on('data', function(chunk) {
chunks.push(chunk);
});
response.on('end', function() {
console.log('Response', Buffer.concat(chunks).toString());
});
});
request.on('error', function(error) {
console.log(error.message);
});
request.end();
#9
3
Node should support using the http_proxy environmental variable - so it is cross platform and works on system settings rather than requiring a per-application configuration.
节点应该支持使用http_proxy环境变量——因此它是跨平台的,可以在系统设置上工作,而不需要每个应用程序配置。
Using the provided solutions, I would recommend the following:
使用所提供的解决方案,我建议如下:
Coffeescript
Coffeescript
get_url = (url, response) ->
if process.env.http_proxy?
match = process.env.http_proxy.match /^(http:\/\/)?([^:\/]+)(:([0-9]+))?/i
if match
http.get { host: match[2], port: (if match[4]? then match[4] else 80), path: url }, response
return
http.get url, response
Javascript
Javascript
get_url = function(url, response) {
var match;
if (process.env.http_proxy != null) {
match = process.env.http_proxy.match(/^(http:\/\/)?([^:\/]+)(:([0-9]+))?/i);
if (match) {
http.get({
host: match[2],
port: (match[4] != null ? match[4] : 80),
path: url
}, response);
return;
}
}
return http.get(url, response);
};
Usage To use the method, effectively just replace http.get, for instance the following writes the index page of google to a file called test.htm:
使用该方法,只需有效地替换http。例如,下面将谷歌的索引页写到一个名为test.htm的文件:
file = fs.createWriteStream path.resolve(__dirname, "test.htm")
get_url "http://www.google.com.au/", (response) ->
response.pipe file
response.on "end", ->
console.log "complete"
#10
1
Imskull's answer almost worked for me, but I had to make some changes. The only real change is adding username, password, and setting rejectUnauthorized to false. I couldn't comment so I put this in an answer.
Imskull的回答几乎对我起了作用,但是我必须做出一些改变。唯一真正的改变是添加用户名、密码和设置rejectUnauthorized为false。我无法评论,所以我把它放在了一个答案里。
If you run the code it'll get you the titles of the current stories on Hacker News, per this tutorial: http://smalljs.org/package-managers/npm/
如果您运行代码,它将为您提供关于Hacker News的当前故事的标题,参见本教程:http://smalljs.org/package-managers/npm/
var cheerio = require('cheerio');
var request = require('request');
request({
'url': 'https://news.ycombinator.com/',
'proxy': 'http://Username:Password@YourProxy:Port/',
'rejectUnauthorized': false
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
if (response.body) {
var $ = cheerio.load(response.body);
$('td.title a').each(function() {
console.log($(this).text());
});
}
} else {
console.log('Error or status not equal 200.');
}
});
#11
1
I bought private proxy server, after purchase I got:
我购买了私人代理服务器,购买后我得到:
255.255.255.255 // IP address of proxy server
99999 // port of proxy server
username // authentication username of proxy server
password // authentication password of proxy server
And I wanted to use it. First answer and second answer worked only for http(proxy) -> http(destination), however I wanted http(proxy) -> https(destination).
我想用它。第一个答案和第二个答案只适用于http(代理)-> http(目的地),但是我想要http(代理)-> https(目的地)。
And for https destination it would be better to use HTTP tunnel directly. I found solution here. Final code:
对于https目的地,最好直接使用HTTP隧道。我发现解决方案。最后的代码:
const http = require('http')
const https = require('https')
const username = 'username'
const password = 'password'
const auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64')
http.request({
host: '255.255.255.255', // IP address of proxy server
port: 99999, // port of proxy server
method: 'CONNECT',
path: 'kinopoisk.ru:443', // some destination, add 443 port for https!
headers: {
'Proxy-Authorization': auth
},
}).on('connect', (res, socket) => {
if (res.statusCode === 200) { // connected to proxy server
https.get({
host: 'www.kinopoisk.ru',
socket: socket, // using a tunnel
agent: false // cannot use a default agent
}, (res) => {
let chunks = []
res.on('data', chunk => chunks.push(chunk))
res.on('end', () => {
console.log('DONE', Buffer.concat(chunks).toString('utf8'))
})
})
}
}).on('error', (err) => {
console.error('error', err)
}).end()
#12
0
May not be the exact one-liner you were hoping for but you could have a look at http://github.com/nodejitsu/node-http-proxy as that may shed some light on how you can use your app with http.Client.
可能不是您所期望的那种一行程序,但您可以查看http://github.com/nodejitsu/node-http-proxy,因为这可能有助于了解如何在http.Client中使用应用程序。
#13
0
http://groups.google.com/group/nodejs/browse_thread/thread/d5aadbcaa00c3f7/12ebf01d7ec415c3?lnk=gst&q=proxy 12 ebf01d7ec415c3
Based on the answers from this thread it would seem like you could use proxychains to run node.js through the proxy server:$ proxychains /path/to/node application.js
根据这个线程的回答,似乎可以使用proxychain来运行node。通过代理服务器的js: $ proxychain /path/to/node application.js
Personally I wasnt able to install any of the proxychains versions on Cygwin/Windows environment so couldn't test it.
我个人无法在Cygwin/Windows环境中安装任何proxychain版本,因此无法对其进行测试。
Furthermore, they also talked about using connect-proxy but I could not find any documentation on how to do this.
此外,他们还讨论了使用connect-proxy,但我找不到任何关于如何使用它的文档。
In short, I'm still stuck, but maybe someone can use this info to find a suitable work-around.
简而言之,我还是被困住了,但是也许有人可以利用这个信息找到合适的工作。
#14
0
For using a proxy with https I tried the advice on this website (using dependency https-proxy-agent) and it worked for me:
对于使用https代理,我尝试了这个网站上的建议(使用依赖关系httpsproxy -agent),它对我起了作用:
http://codingmiles.com/node-js-making-https-request-via-proxy/
http://codingmiles.com/node-js-making-https-request-via-proxy/
#15
0
use 'https-proxy-agent' like this
使用“https-proxy-agent”这样的
var HttpsProxyAgent = require('https-proxy-agent');
var proxy = process.env.https_proxy || 'other proxy address';
var agent = new HttpsProxyAgent(proxy);
options = {
//...
agent : agent
}
https.get(options, (res)=>{...});
#16
0
Here just a simple sample if you don't want to use crypto or Buffer. You could make base64 encode of -> myuser:mypassword, and then add "Basic " in the beginning. That's the value of "Proxy-Authorization" header.
这里只是一个简单的示例,如果您不想使用加密或缓冲区的话。您可以将base64编码为-> myuser:mypassword,然后在开头添加“Basic”。这是“代理授权”头的值。
var Http = require('http');
var req = Http.request({
host: 'myproxy.com.zx',
port: 8080,
headers:{"Proxy-Authorization": "Basic bXl1c2VyOm15cGFzcw"},
method: 'GET',
path: 'http://www.google.com/'
}, function (res) {
res.on('data', function (data) {
console.log(data.toString());
});
});
req.end();