Javascript 使用localhost作为http代理

Javascript 使用localhost作为http代理,javascript,node.js,http-proxy,Javascript,Node.js,Http Proxy,我正在尝试创建一个localhost Http代理,但到目前为止没有任何效果。我正在尝试使用端口8200上托管的localhost代理向其他URL发送请求。到目前为止,我已经使用 我使用请求npm库发送请求 const request = require('request'); var req = request.get({ url: site, proxy: `http://127.0.0.1:8200`, timeout: 10000

我正在尝试创建一个localhost Http代理,但到目前为止没有任何效果。我正在尝试使用端口8200上托管的localhost代理向其他URL发送请求。到目前为止,我已经使用

我使用请求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代理,我们还不太清楚

第一个.listen在您的代码中并没有真正设置代理服务器,即使它连接到.createProxyServer。相反,它只是创建一个常规http服务器,就像.createServer之后的第二个.listen一样

基于s,我把它弄得有点乱,并把它作为代理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);

使用一些用于该作业的代理软件。Fiddler适用于Windows,Linux和MacOS也有测试版。我试图避免使用外部软件,因为我想将其打包到我的电子应用程序中。是否可以让它处理https?是的,我链接的文档中也有。它需要创建SSL证书和一些额外的配置。
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);