Skip to content

Commit 5ebf983

Browse files
committed
Merge pull request #642 from bruce-one/proxyRes-req-res
`proxyRes` event, provide access to the req and res objects
2 parents c472527 + 1385635 commit 5ebf983

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/http-proxy/passes/web-incoming.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ web_o = Object.keys(web_o).map(function(pass) {
131131
(options.buffer || req).pipe(proxyReq);
132132

133133
proxyReq.on('response', function(proxyRes) {
134-
if(server) { server.emit('proxyRes', proxyRes); }
134+
if(server) { server.emit('proxyRes', proxyRes, req, res); }
135135
for(var i=0; i < web_o.length; i++) {
136136
if(web_o[i](req, res, proxyRes)) { break; }
137137
}

test/lib-http-proxy-passes-web-incoming-test.js

+28
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,32 @@ describe('#createProxyServer.web() using own http server', function () {
127127
method: 'GET',
128128
}, function() {}).end();
129129
});
130+
131+
it('should proxy the request and provide a proxyRes event with the request and response parameters', function(done) {
132+
var proxy = httpProxy.createProxyServer({
133+
target: 'http://127.0.0.1:8080'
134+
});
135+
136+
function requestHandler(req, res) {
137+
proxy.once('proxyRes', function (proxyRes, pReq, pRes) {
138+
source.close();
139+
proxyServer.close();
140+
expect(pReq).to.be.equal(req);
141+
expect(pRes).to.be.equal(res);
142+
done();
143+
});
144+
145+
proxy.web(req, res);
146+
}
147+
148+
var proxyServer = http.createServer(requestHandler);
149+
150+
var source = http.createServer(function(req, res) {
151+
res.end('Response');
152+
});
153+
154+
proxyServer.listen('8084');
155+
source.listen('8080');
156+
http.request('http://127.0.0.1:8084', function() {}).end();
157+
});
130158
});

0 commit comments

Comments
 (0)