2011-09-25 17 views
8

apiに対してwebappを開発しました。 apiがローカルシステム上で動作していないので、私はクロスドメインの問題で動作しないように要求をプロキシする必要があります。これを行う簡単な方法はありますか?私のindex.htmlはローカルから送信され、他のすべてのGET、POST、PUT、DELETEリクエストはxyz.net/apiEndPointに送られます。nodejsを使用したプロキシ

編集:

これは、インデックス、JS、CSSファイルを提供が、外部APIへのルート残りをいけないだろう、私の最初のソリューションが、didntの仕事

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) { 
    proxy.proxyRequest(req, res, { 
     host: 'http://apiUrl', 
     port: 80 
    }); 

}); 

です。これは、私が持っているエラーメッセージです:

An error has occurred: {"stack":"Error: ENOTFOUND, Domain name not found\n at IOWatcher.callback (dns.js:74:15)","message":"ENOTFOUND, Domain name not found","errno":4,"code":"ENOTFOUND"} 

答えて

4

を見てくださいreadme for http-proxyを見てください。それはproxyRequestをコールする方法の例があります。

proxy.proxyRequest(req, res, { 
    host: 'localhost', 
    port: 9000 
}); 

あなたはproxyRequest方法に偽のドメイン名を渡しているように聞こえるのエラーメッセージに基づいて。

+0

ホストは「http://」で始まります。これを削除すると修正されます。 HTTP/1.1 400 Bad Request X-Powered-By:Express サーバー:Apache-Coyote/1.1 コンテンツの長さ:0 date:Sun、25 Sep 2011 18:06: 22 GMT 接続:閉じる –

+0

@AndreasKöberle、まず、この質問を編集するか、新しい質問を開くことをおすすめします。 –

+1

私は新しい質問をしました。 http://stackoverflow.com/questions/7559862/proxy-with-nodejs-and-express –

0

多分あなたは私の答えhereを見てください。ここでは、要求パッケージを使用してすべての要求をプロキシにパイプし、応答を要求元にパイプします。

2

その他の回答は若干古くなっています。ここで

は急行で1.0 HTTPプロキシを使用する方法は次のとおりです。ここで

var httpProxy = require('http-proxy'); 

var apiProxy = httProxy.createProxyServer(); 

app.get("/api/*", function(req, res){ 
    apiProxy.web(req, res, { target: 'http://google.com:80' }); 
}); 
1

は、リクエスト/レスポンスボディを変更することができ、プロキシの一例です。

透明な読み書きストリームを実装するモジュールthroughを評価します。

var http = require('http'); 
var through = require('through'); 

http.createServer(function (clientRequest, clientResponse) { 

    var departureProcessor = through(function write(requestData){ 

     //log or modify requestData here 
     console.log("=======REQUEST FROM CLIENT TO SERVER CAPTURED========"); 
     console.log(requestData); 

     this.queue(requestData); 
    }); 

    var proxy = http.request({ hostname: "myServer.com", port: 80, path: clientRequest.url, method: 'GET'}, function (serverResponse) { 

     var arrivalProcessor = through(function write(responseData){ 

      //log or modify responseData here 
      console.log("======RESPONSE FROM SERVER TO CLIENT CAPTURED======"); 
      console.log(responseData); 

      this.queue(responseData); 
     }); 

     serverResponse.pipe(arrivalProcessor); 
     arrivalProcessor.pipe(clientResponse); 
    }); 

    clientRequest.pipe(departureProcessor, {end: true}); 
    departureProcessor.pipe(proxy, {end: true}); 

}).listen(3333); 
関連する問題