0
私はnode.jsを学ぼうとしており、私のコンテキストはnodejitsuのhttp-proxyモジュールです。node.js、httpプロキシリクエストの最後にコールバック?
プロキシイベントの最後にアクセスしたいので、ターゲットサーバーが応答するまでの時間を記録できます。しかし、適切なコールバックやイベントハンドラを作成する方法はわかりません。私はHTTPプロキシのソースを見てきましたが、従来のコールバックメソッドを使用していないようです。
/* demonstrate a basic proxy server that can log the time
it takes to execute a given hit on the destination server
*/
var http = require('http'),
httpProxy = require('http-proxy');
// Create the proxy server
httpProxy.createServer(function (req, res, proxy) {
var startTime = (new Date()).getTime();
// NEED HELP HERE.
// something like this should do it?
proxy.addListener("end",function() {
var endTime = (new Date()).getTime();
console.log("request took " + (endTime - startTime) + "ms");
});
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 9000
});
}).listen(8000);
// the destination server
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
// make the hit slow by pausing the end()
setTimeout(function() {
res.end();
},2000);
}).listen(9000);