我已經用nodejs,express和htt-proxy編寫了一個小代理。它可以很好地用於服務本地文件,但無法當它涉及到代理的外部API:使用快速代理路由沒有響應
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
});
});
的問題是,有沒有從雅虎API響應,也許有一個迴應,但我不的上前瀏覽器。
任何機會,你可以解釋'req.pipe(request(url))。pipe(res);'?爲什麼需要兩個「管道」? – Jonathan
當請求有json內容類型時,'pipe'行會掛起 - 請參見[this question](http://stackoverflow.com/questions/26121830/proxy-json-requests-with-node-express) – Jonathan
爲什麼不使用'request(url).pipe(res);'as [這裏建議](http://stackoverflow.com/a/16924410/348545) – Jonathan