2013-03-15 142 views
1

我們的服務器在沒有代理服務器的情況下發起http呼叫。 是否可以通過代理服務器進行相同的呼叫? (我們想用的請求。)通過代理服務器請求http呼叫

例如代理服務器:112.175.18.180端口80個

app.js:

var http = require('http'); 

http.createServer(function (req, res) { 
    res.writeHead(200, {'Content-Type': 'text/plain'}); 
    res.end('Hello World\n'); 
}).listen(1337, '127.0.0.1'); 
console.log('Server running at http://127.0.0.1:1337/'); 



var request = require("request"); 

var parseMyAwesomeHtml = function(html) { 
    console.log(html); 
}; 

request("http://checkip.dyndns.org/", function (error, response, body) { 
    if (!error) 
     parseMyAwesomeHtml(body); 
    else 
     console.log(error); 
}); 

回答

1

我們找到了正確的語法。 工程很棒。

app.js:

var http = require('http'); 
var request = require("request"); 

http.createServer(function (req, res) { 
    res.writeHead(200, {'Content-Type': 'text/plain'}); 
    res.end('Hello World\n'); 
}).listen(1337, '127.0.0.1'); 
console.log('Server running at http://127.0.0.1:1337/'); 


var parseMyAwesomeHtml = function(html) { 
    console.log(html); 
}; 



request({uri:"http://checkip.dyndns.org/",proxy: 'http://112.175.18.180/', port: 80 }, function (error, response, body) { 
    if (!error) 
     parseMyAwesomeHtml(body); 
    else 
     console.log(error); 
}); 
+0

代理不會永遠工作。拿一個新的代理進行測試。 – 2013-03-15 19:35:19

+0

你應該接受你的答案。 – generalhenry 2013-03-16 01:02:22

+0

@generalhenry right,thx – 2013-03-22 10:49:24