如何使用http-proxy使下面的代码创建代理服务器,发送请求,并获得对外界的响应?

问题描述

对于code at node-http-proxy

var http = require('http'),httpProxy = require('http-proxy');
 
//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createProxyServer({});
 
//
// Create your custom server and just call `proxy.web()` to proxy
// a web request to the target passed in the options
// also you can use `proxy.ws()` to proxy a websockets request
//
var server = http.createServer(function(req,res) {
  // You can define here your custom logic to handle the request
  // and then proxy the request.
  proxy.web(req,res,{ target: 'http://127.0.0.1:5050' });
});
 
console.log("listening on port 5050")
server.listen(5050);

是否应该在 localhost、端口 5050 创建一个代理服务器,以便将流量路由到外部世界并获得响应?

如果我运行这个程序,并在 Firefox 中设置为使用代理“本地主机和端口 5050”并尝试不使用 https 的网站之一,例如 http://apache.org/ 上面的程序会崩溃错误

listening on port 5050
/Users/peter/code/node_modules/http-proxy/lib/http-proxy/index.js:120
    throw err;
    ^

Error: connect EMFILE 127.0.0.1:5050 - Local (undefined:undefined)
    at internalConnect (net.js:908:16)
    at defaultTriggerAsyncIdScope (internal/async_hooks.js:301:12)
    at net.js:999:9
    at processticksAndRejections (internal/process/task_queues.js:75:11) {
  errno: 'EMFILE',code: 'EMFILE',syscall: 'connect',address: '127.0.0.1',port: 5050
}

如何让它发挥作用?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)