2011-09-26 10 views
22

私は小規模なプロキシをnodejs、express、およびhtt-proxyで記述しました。中に思いついたエクスプレスプロキシルートを使用した応答がありません

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からの応答がないということであるかもしれない応答があるが、私はいけない:それは、ローカルファイルを提供するためにうまく動作しますが、それは外部APIへのプロキシになると失敗しますブラウザ。

答えて

92

piperequest - パッケージ

var request = require('request'); 

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

このAPIへのパイプ全体の要求をし、パイプ戻ってリクエスタに応答してさらに簡単。これはまた、あなたがまた、クエリ文字列を気にした場合は、パイプ、それだけでなく

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

あなたは 'req.pipe(request(url))。pipe(res);'で何が起こるかを説明することができます。 2つの 'パイプ'が必要なのはなぜですか? – Jonathan

+3

リクエストにjsonのcontent-typeがあると 'pipe'行がハングします - [この質問](http://stackoverflow.com/questions/26121830/proxy-json-requests-with-node-express) – Jonathan

+0

上の 'request(url).pipe(res);'のみを使用して 'request(url).pipe(res);' [ここに示唆されている](http://stackoverflow.com/a/16924410/348545) – Jonathan

7

あなたがテストしているが、私は次のように使用してコードのサンプルと同じURLを照会していたときに多分あなたのコードが異なっている:

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

と私は戻って何を取得します。私の推測では、あなたが(8080から)ポートを80に変更したいです - 私はそうのようにそれを変更すると、それは動作します:

http://query.yahooapis.com:80/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: 80 
}); 
+0

[OK]を、ポートを変更するだけでなく、私のプロジェクトのために働くべきでPOST/PUT/DELETEおよび他のすべての要求\ O/

処理しますが、残念ながら私はサーバーから404を持っています。ブラウザで同じURLを呼び出すと動作します。 –

4

たぶん私はhttp-proxyを間違った方法で使用してください。 restlerを使用すると、私が欲しいものを行います。

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) 
      }); 

}); 
関連する問題