【发布时间】:2019-08-09 03:50:09
【问题描述】:
我正在尝试创建一个 localhost Http 代理,但到目前为止没有任何效果。我要做的是使用托管在端口 8200 上的 localhost 代理向不同的 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);
我使用 requests 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
是否可以创建 localhost HTTP 代理?如果是这样,我应该怎么做?
【问题讨论】:
-
使用一些用于这项工作的代理软件。 Fiddler 适用于 Windows,也有适用于 Linux 和 MacOS 的 beta 版本。
-
我试图避免使用外部软件,因为我希望将它打包到我的电子应用程序中。
标签: javascript node.js http-proxy