使用 localhost 作为 http 代理
Use localhost as an http proxy
我正在尝试创建本地主机 Http 代理,但到目前为止没有任何效果。我想做的是使用托管在端口 8200 上的本地主机代理向不同的 URL 发送请求。到目前为止,我已经尝试过使用 this node library
var http = require('http'),
httpProxy = require('http-proxy');
//
// Create your proxy server and set the target in the options.
//
httpProxy.createProxyServer({target:'https://www.google.com/recaptcha/api.js'}).listen(8200); // See (†)
//
// Create your target server
//
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
我使用请求 npm 库发送请求。
const request = require('request');
var req = request.get({
url: site,
proxy: `http://127.0.0.1:8200`,
timeout: 10000
})
我收到的错误是这样的:
Error: tunneling socket could not be established, cause=connect ECONNREFUSED 127.0.0.1:8200
at ClientRequest.onError (/Users/project/node_modules/tunnel-agent/index.js:177:17)
at Object.onceWrapper (events.js:282:20)
at ClientRequest.emit (events.js:194:13)
at Socket.socketErrorListener (_http_client.js:401:9)
at Socket.emit (events.js:194:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at processTicksAndRejections (internal/process/task_queues.js:81:17)
Error: tunneling socket could not be established, cause=socket hang up
at ClientRequest.onError (/Users/project/node_modules/tunnel-agent/index.js:177:17)
at Object.onceWrapper (events.js:282:20)
at ClientRequest.emit (events.js:194:13)
at Socket.socketOnEnd (_http_client.js:435:9)
at Socket.emit (events.js:199:15)
at endReadableNT (_stream_readable.js:1141:12)
at processTicksAndRejections (internal/process/task_queues.js:81:17)
Error: tunneling socket could not be established, cause=socket hang up
at ClientRequest.onError (/Users/project/node_modules/tunnel-agent/index.js:177:17)
at Object.onceWrapper (events.js:282:20)
at ClientRequest.emit (events.js:194:13)
at Socket.socketOnEnd (_http_client.js:435:9)
at Socket.emit (events.js:199:15)
at endReadableNT (_stream_readable.js:1141:12)
at processTicksAndRejections (internal/process/task_queues.js:81:17)
Error: tunneling socket could not be established, cause=read ECONNRESET
是否可以创建本地主机 HTTP 代理?如果是这样,我应该怎么做?
http-proxy
docs 并不清楚如何设置动态 http 代理。
您代码中的第一个 .listen()
并没有真正设置代理服务器,即使它附加到 .createProxyServer()
。相反,它只是创建一个常规的 http 服务器,就像 .createServer()
.
之后的第二个 .listen()
基于 examples,我稍微弄乱了它并想出了这个作为代理 http 请求的最低限度。 (它不处理 https,我不确定它可能有哪些其他限制):
var http = require('http'),
httpProxy = require('http-proxy');
/* This creates a kind of agent used to fetch content on behalf of an
http proxy request.
*/
var proxy = httpProxy.createProxyServer();
/* This creates a regular web server. Http proxies still use http,
just a little differently than usual.
*/
http.createServer(function (req, res) {
/* Urls in proxy requests includes parts, like domain and port, that aren't
included in the `GET` of regular http requests.
*/
/* Serve up some content. Normally this would be some page content or
something but, since we're supposed to be an http proxy, we tell `proxy`
to handle the request.
*/
proxy.web(req, res, { target: req.url });
}).listen(8200);
我正在尝试创建本地主机 Http 代理,但到目前为止没有任何效果。我想做的是使用托管在端口 8200 上的本地主机代理向不同的 URL 发送请求。到目前为止,我已经尝试过使用 this node library
var http = require('http'),
httpProxy = require('http-proxy');
//
// Create your proxy server and set the target in the options.
//
httpProxy.createProxyServer({target:'https://www.google.com/recaptcha/api.js'}).listen(8200); // See (†)
//
// Create your target server
//
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
我使用请求 npm 库发送请求。
const request = require('request');
var req = request.get({
url: site,
proxy: `http://127.0.0.1:8200`,
timeout: 10000
})
我收到的错误是这样的:
Error: tunneling socket could not be established, cause=connect ECONNREFUSED 127.0.0.1:8200
at ClientRequest.onError (/Users/project/node_modules/tunnel-agent/index.js:177:17)
at Object.onceWrapper (events.js:282:20)
at ClientRequest.emit (events.js:194:13)
at Socket.socketErrorListener (_http_client.js:401:9)
at Socket.emit (events.js:194:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at processTicksAndRejections (internal/process/task_queues.js:81:17)
Error: tunneling socket could not be established, cause=socket hang up
at ClientRequest.onError (/Users/project/node_modules/tunnel-agent/index.js:177:17)
at Object.onceWrapper (events.js:282:20)
at ClientRequest.emit (events.js:194:13)
at Socket.socketOnEnd (_http_client.js:435:9)
at Socket.emit (events.js:199:15)
at endReadableNT (_stream_readable.js:1141:12)
at processTicksAndRejections (internal/process/task_queues.js:81:17)
Error: tunneling socket could not be established, cause=socket hang up
at ClientRequest.onError (/Users/project/node_modules/tunnel-agent/index.js:177:17)
at Object.onceWrapper (events.js:282:20)
at ClientRequest.emit (events.js:194:13)
at Socket.socketOnEnd (_http_client.js:435:9)
at Socket.emit (events.js:199:15)
at endReadableNT (_stream_readable.js:1141:12)
at processTicksAndRejections (internal/process/task_queues.js:81:17)
Error: tunneling socket could not be established, cause=read ECONNRESET
是否可以创建本地主机 HTTP 代理?如果是这样,我应该怎么做?
http-proxy
docs 并不清楚如何设置动态 http 代理。
您代码中的第一个 .listen()
并没有真正设置代理服务器,即使它附加到 .createProxyServer()
。相反,它只是创建一个常规的 http 服务器,就像 .createServer()
.
.listen()
基于 examples,我稍微弄乱了它并想出了这个作为代理 http 请求的最低限度。 (它不处理 https,我不确定它可能有哪些其他限制):
var http = require('http'),
httpProxy = require('http-proxy');
/* This creates a kind of agent used to fetch content on behalf of an
http proxy request.
*/
var proxy = httpProxy.createProxyServer();
/* This creates a regular web server. Http proxies still use http,
just a little differently than usual.
*/
http.createServer(function (req, res) {
/* Urls in proxy requests includes parts, like domain and port, that aren't
included in the `GET` of regular http requests.
*/
/* Serve up some content. Normally this would be some page content or
something but, since we're supposed to be an http proxy, we tell `proxy`
to handle the request.
*/
proxy.web(req, res, { target: req.url });
}).listen(8200);