2011-09-26 33 views
22

Tôi đã viết một proxy nhỏ với các nút node, express và htt-proxy. Nó hoạt động tốt phục vụ tập tin địa phương nhưng không thành công khi nói đến proxy để api bên ngoài:Không có phản ứng nào bằng cách sử dụng đường dẫn proxy nhanh

var express = require('express'), 
    app = express.createServer(), 
    httpProxy = require('http-proxy'); 


app.use(express.bodyParser()); 
app.listen(process.env.PORT || 1235); 

var proxy = new httpProxy.RoutingProxy(); 

app.get('/', function(req, res) { 
    res.sendfile(__dirname + '/index.html'); 
}); 
app.get('/js/*', function(req, res) { 
    res.sendfile(__dirname + req.url); 
}); 
app.get('/css/*', function(req, res) { 
    res.sendfile(__dirname + req.url); 
}); 

app.all('/*', function(req, res) { 
    req.url = 'v1/public/yql?q=show%20tables&format=json&callback='; 
    proxy.proxyRequest(req, res, { 
     host: 'query.yahooapis.com', //yahoo is just an example to verify its not the apis fault 
     port: 8080 
    }); 

}); 

Vấn đề là không có phản hồi từ api yahoo, có thể có một phản ứng nhưng tôi không nghĩ ra trong trình duyệt.

Trả lời

92

Thậm chí đơn giản với piperequest -Package

var request = require('request'); 

app.use('/api', function(req, res) { 
    var url = apiUrl + req.url; 
    req.pipe(request(url)).pipe(res); 
}); 

Nó Ống toàn bộ yêu cầu API và đường ống phản hồi trở lại để người yêu cầu. Đây cũng xử lý POST/PUT/DELETE và tất cả các yêu cầu khác \ o/

Nếu bạn cũng quan tâm đến chuỗi truy vấn bạn nên ống nó cũng

req.pipe(request({ qs:req.query, uri: url })).pipe(res); 
+0

bất kỳ cơ hội nào bạn có thể giải thích những gì xảy ra trong 'req.pipe (yêu cầu (url)). Pipe (res);'? Tại sao hai yêu cầu của 'pipe'? – Jonathan

+3

dòng 'đường ống 'bị treo khi yêu cầu có loại nội dung json - xem [câu hỏi này] (http://stackoverflow.com/questions/26121830/proxy-json-requests-with-node-express) – Jonathan

+0

tại sao không sử dụng 'request (url) .pipe (res);' như [được đề xuất ở đây] (http://stackoverflow.com/a/16924410/348545) – Jonathan

7

Có lẽ mã của bạn là khác nhau khi bạn đang thử nghiệm, nhưng tôi truy vấn URL tương tự như trong mẫu mã của bạn bằng cách sử dụng sau đây:

http://query.yahooapis.com:8080/v1/public/yql?q=show%20tables&format=json&callback=

và tôi nhận được gì trở lại. Tôi đoán là bạn muốn thay đổi cổng 80 (từ 8080) - nó hoạt động khi tôi thay đổi nó như vậy:

http://query.yahooapis.com:80/v1/public/yql?q=show%20tables&format=json&callback=

Vì vậy, có nghĩa là nó nên là:

proxy.proxyRequest(req, res, { 
    host: 'query.yahooapis.com', //yahoo is just an example to verify its not the apis fault 
    port: 80 
}); 
+0

Ok, thay đổi cổng làm việc cho dự án của tôi là tốt nhưng tiếc là tôi đã có một 404 từ máy chủ. Gọi cùng một url trong trình duyệt nó hoạt động. –

4

Có lẽ tôi sử dụng proxy http theo cách sai. Sử dụng restler làm những gì tôi muốn:

var express = require('express'), 
    app = express.createServer(), 
    restler = require('restler'); 


app.use(express.bodyParser()); 
app.listen(1234); 



app.get('/', function(req, res) { 
    console.log(__dirname + '/index.html') 
    res.sendfile(__dirname + '/index.html'); 
}); 
app.get('/js/*', function(req, res) { 
    res.sendfile(__dirname + req.url); 
}); 
app.get('/css/*', function(req, res) { 
    res.sendfile(__dirname + req.url); 
}); 


app.all('/*', function(req, res) { 

    restler.get('http://myUrl.com:80/app_test.php/api' + req.url, { 

     }).on('complete', function (data) { 
       console.log(data) 
       res.json(data) 
      }); 

}); 
Các vấn đề liên quan