Я пытаюсь выучить node.js, и мой контекст - это модуль http-proxy из nodejitsu.
Я хотел бы получить доступ к концу прокси-события, чтобы я мог регистрировать, сколько времени целевой сервер ответил, чтобы ответить. Однако я не могу понять, как создать соответствующий обратный вызов или обработчик событий. Я посмотрел на источник для 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);