2011-10-29 7 views
2

次のnode.js HTTPプロキシ実装のどれがパフォーマンスが良いのでしょうか?どのnode.js HTTPプロキシの実装がより効果的ですか?

最初の実装は次のとおりです。

var http = require('http'); 

http.createServer(function(request, response) { 
    var proxy = http.createClient(80, "google.com") 
    var proxy_request = proxy.request(request.method, request.url, request.headers); 
    proxy_request.addListener('response', function (proxy_response) { 
    proxy_response.addListener('data', function(chunk) { 
     response.write(chunk, 'binary'); 
    }); 
    proxy_response.addListener('end', function() { 
     response.end(); 
    }); 
    response.writeHead(proxy_response.statusCode, proxy_response.headers); 
    }); 
    request.addListener('data', function(chunk) { 
    proxy_request.write(chunk, 'binary'); 
    }); 
    request.addListener('end', function() { 
    proxy_request.end(); 
    }); 
}).listen(8080); 

秒1はstream.pipe()を使用し、それはのようだ:

var http = require('http'); 

http.createServer(function(request, response) { 
    var proxy = http.createClient(80, "google.com"); 
    var proxy_request = proxy.request(request.method, request.url, request.headers); 
    proxy_request.on('response', function (proxy_response) { 
     proxy_response.pipe(response); 
     response.writeHead(proxy_response.statusCode, proxy_response.headers); 
    }); 

    request.pipe(proxy_request); 
}).listen(8080); 
+1

がベンチマークを実行します。私はパイプでそれに賭けた。 –

+1

なぜあなたは車輪を再発明したいのですか?あなたはあなたのために仕事をするモジュールを使うことができます。 http://search.npmjs.org/でnode.jsモジュールを検索します。 –

答えて

5

ファイルが大きいと、クライアント接続がある場合は最初のものは、あなたのプロセスを爆破かもしれませんアップロードされたファイルが大きく、サーバーのアップロード帯域幅が小さい場合は遅くなります。 pipeを使用してください。この種類のもの用に設計されています。

また、このためにNPMから既存のモジュールを使用します。

  • nodejitsuの生産に使用される多くの機能と:http-proxy
  • 速い:bouncy
関連する問題