3

i made a small forward proxy with nodejs and hosted it in appfog. it's working in local after setting up the proxy of my browser, but when i try using the one hosted in appfog it's says: *Errore 130 (net::ERR_PROXY_CONNECTION_FAILED): Connessione al server proxy non riuscita.* this is my code:

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);

Am i missing something?

your code is working but once i've tried using it like this:

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);

It's still doesn't work: i got request timed out when i try to ping the address, i got the same ERR_PROXY_CONNECTION_FAILED... locally work but when i using the remote address as proxy it doesn't

MkM
  • 201
  • 4
  • 11

1 Answers1

2

First: The app needs to use the port number issued to it by cloudfoundry. The app sits behind a reverse proxy that takes incoming requests on port 80 and forwards them to the VCAP_APP_PORT.

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

....

}).listen(port);

Then you access your hosted app like this:

http://<app_name>.<infra>.af.cm  // port 80

And your local app:

http://localhost:8080

Second: You may need to use a options hash to send to the http.get method instead of just supplying the request.url.

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

I tested the following code on my local box and on AppFog and the IPs were different. Whatismyip returned my local interent ip address when running locally and on the AppFog hosted app it returned the AppFog server ip.

var port = process.env.VCAP_APP_PORT || 8080;
var http = require('http');
var options = {
    host: "www.whatismyip.com",
    port: 80,
    path: '/',
    method: 'GET'
  };
http.createServer(function(request, response) {
  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);
Tim Santeford
  • 27,385
  • 16
  • 74
  • 101
  • hi, thanks for the answer but i already tried it and when i go to a site when i can test my ip, i got the same ip with and without using the proxy.. i'm doing a new http request on the server so i have too see the server ip and not mine is that right? – MkM Nov 23 '12 at 08:34
  • I tried with your code using dynamics url but it doesn't work, can you see my message again... thanks – MkM Nov 26 '12 at 13:48