Node.js nodejs中的转发代理

Node.js nodejs中的转发代理,node.js,http,proxy,reverse-proxy,appfog,Node.js,Http,Proxy,Reverse Proxy,Appfog,我用nodejs制作了一个小的前向代理,并将其托管在appfog中。 设置浏览器的代理后,它在本地工作,但当我尝试使用appfog中托管的代理时,它会显示: *错误130(网络::错误代理连接失败):连接服务器代理非riuscita* 这是我的代码: var http = require('http'); http.createServer(function(request, response) { http.get(request.url, function(res) { console.

我用nodejs制作了一个小的前向代理,并将其托管在appfog中。 设置浏览器的代理后,它在本地工作,但当我尝试使用appfog中托管的代理时,它会显示: *错误130(网络::错误代理连接失败):连接服务器代理非riuscita* 这是我的代码:

var http = require('http');
http.createServer(function(request, response) {
http.get(request.url, function(res) {
  console.log("Got response: " + res.statusCode);
    res.on('data', function(d) {
      response.write(d);
  });
   res.on('end', function() {
     response.end();
    });
}).on('error', function(e) {
  console.log("Got error: " + e.message);
});
}).listen(8080);
我错过什么了吗

您的代码正在工作,但一旦我尝试这样使用它:

var port = process.env.VCAP_APP_PORT || 8080;
var http = require('http');
var urldec = require('url');
http.createServer(function(request, response) {
var gotourl=urldec.parse(request.url);
var hosturl=gotourl.host;
var pathurl=gotourl.path;
var options = {
    host: hosturl,
    port: 80,
    path: pathurl,
    method: request.method
  };

  http.get(options, function(res) {
    console.log("Got response: " + res.statusCode);
    res.on('data', function(d) {
      response.write(d);
    });
    res.on('end', function() {
      response.end();
    });
  }).on('error', function(e) {
    console.log("Got error: " + e.message);
    response.write("error");
    response.end();
  });
}).listen(port);
console.log(port);
http://<app_name>.<infra>.af.cm  // port 80

它仍然不起作用:当我尝试ping地址时,请求超时,我得到相同的错误\u代理\u连接\u失败。。。本地工作,但当我使用远程地址作为代理时,它不会首先:

应用程序需要使用cloudfoundry发给它的端口号。该应用位于反向代理之后,该代理接收端口80上的传入请求,并将它们转发到VCAP_app_端口

var port = process.env.VCAP_APP_PORT || 8080; // 8080 only works on localhost

....

}).listen(port);
然后按如下方式访问托管应用程序:

var port = process.env.VCAP_APP_PORT || 8080;
var http = require('http');
var urldec = require('url');
http.createServer(function(request, response) {
var gotourl=urldec.parse(request.url);
var hosturl=gotourl.host;
var pathurl=gotourl.path;
var options = {
    host: hosturl,
    port: 80,
    path: pathurl,
    method: request.method
  };

  http.get(options, function(res) {
    console.log("Got response: " + res.statusCode);
    res.on('data', function(d) {
      response.write(d);
    });
    res.on('end', function() {
      response.end();
    });
  }).on('error', function(e) {
    console.log("Got error: " + e.message);
    response.write("error");
    response.end();
  });
}).listen(port);
console.log(port);
http://<app_name>.<infra>.af.cm  // port 80
Second:您可能需要使用选项哈希来发送到http.get方法,而不仅仅是提供request.url

var options = {
  host: '<host part of url without the protocal prefix>',
  path: '<path part of url>'
  port: 80, 
  method: 'GET' }

嗨,谢谢你的回答,但我已经尝试过了,当我去一个网站,当我可以测试我的ip,我得到了相同的ip使用和不使用代理。。我正在服务器上执行一个新的http请求,因此我也看到了服务器ip,而不是我的,对吗?我尝试使用dynamics url使用您的代码,但它不起作用,您能再次看到我的消息吗。。。谢谢