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