2013-05-20 25 views
0

我們希望通過appfog上的node.js代理上網。簡單的代理服務器,可以在node.js主機上工作,如appfog

我們嘗試這種代碼:

proxy2a.js:

var http = require('http'); 

var port = process.env.VCAP_APP_PORT || 8080; 
    console.log ('the portnumber is: '+ port) ; 
http.createServer(function(request, response) { 
    var proxy = http.createClient(80, "checkip.dyndns.org"); 
    var proxy_request = proxy.request(request.method, request.url, request.headers); 
    proxy_request.on('response', function (proxy_response) { 
     proxy_response.pipe(response); 
     response.writeHead(proxy_response.statusCode, proxy_response.headers); 
     console.log(proxy_response.statusCode) ; 
    }); 

    request.pipe(proxy_request); 
}).listen(port); 

的package.json:

{ 
    "name": "proxy2a", 
    "author": "parker", 
    "version": "0.0.0-14", 
    "dependencies": { 



    }, 
    "devDependencies": {}, 
    "optionalDependencies": {}, 
    "engines": { 
     "node": "0.6.x", 
     "iisnode": "*" 
    }, 
    "scripts": { 
     "start": "node proxy2a.js" 
    } 
} 

我們必須從這個question代碼。 (實際上我們只需要服務器訪問一個網站,所以這臺服務器只能服務一個) 這個代碼在我們的本地機器上運行服務器時效果很好(使用節點版本0.6.x.) 但是當我們部署到appfog時,沒有任何反應。 (我們通過ping另一個控制檯的部署url來獲取IP。)你知道如何讓這樣的代理服務器在appfog或任何其他node.js主機上工作嗎?

+0

[這個問題](HTTP:// stackoverflow.com/questions/16638317/why-is-my-simple-node-js-proxy-not-working-when-i-deploy-to-heroku-or-to-appfog)也許會讓你感興趣。 –

回答

2

您的代碼完全按照原樣工作。我複製你的兩個文件,就是奔着以下commands--

af login 
// enter my creds 
af update testproxy 

隨後前往http://testproxy.aws.af.cm/和它的作品。你確定你正確地上傳你的應用,使用了正確的域名,並且你的應用已經啓動了嗎?

(VCAP_APP_PORT被設置爲80,所以您的應用將在端口80上由於此線運行的:var =端口|| process.env.VCAP_APP_PORT 8080)

+0

是的這現在完美。我試圖通過更改我的互聯網連接代理設置訪問代理,哈哈哈。只要我點擊你的鏈接,我就會明白它是如何工作的。 –

相關問題