Skip to content

Commit db5f295

Browse files
committed
[api] Add event-based ability to modify pre-flight proxy requests.
1 parent ed9e12b commit db5f295

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

README.md

+37
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,43 @@ console.log("listening on port 5050")
114114
server.listen(5050);
115115
```
116116

117+
#### Setup a stand-alone proxy server with proxy request header re-writing
118+
This example shows how you can proxy a request using your own HTTP server that
119+
modifies the outgoing proxy request by adding a special header.
120+
121+
```js
122+
var http = require('http'),
123+
httpProxy = require('http-proxy');
124+
125+
//
126+
// Create a proxy server with custom application logic
127+
//
128+
var proxy = httpProxy.createProxyServer({});
129+
130+
// To modify the proxy connection before data is sent, you can listen
131+
// for the 'proxyReq' event. When the event is fired, you will receive
132+
// the following arguments:
133+
// (http.ClientRequest proxyReq, http.IncomingMessage req,
134+
// http.ServerResponse res, Object options). This mechanism is useful when
135+
// you need to modify the proxy request before the proxy connection
136+
// is made to the target.
137+
//
138+
proxy.on('proxyReq', function(proxyReq, req, res, options) {
139+
proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
140+
});
141+
142+
var server = require('http').createServer(function(req, res) {
143+
// You can define here your custom logic to handle the request
144+
// and then proxy the request.
145+
proxy.web(req, res, {
146+
target: 'http://127.0.0.1:5060'
147+
});
148+
});
149+
150+
console.log("listening on port 5050")
151+
server.listen(5050);
152+
```
153+
117154
#### Setup a stand-alone proxy server with latency
118155

119156
```js

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

+5
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ web_o = Object.keys(web_o).map(function(pass) {
109109
common.setupOutgoing(options.ssl || {}, options, req)
110110
);
111111

112+
// Enable developers to modify the proxyReq before headers are sent
113+
proxyReq.on('socket', function(socket) {
114+
if(server) { server.emit('proxyReq', proxyReq, req, res, options); }
115+
});
116+
112117
// allow outgoing socket to timeout so that we could
113118
// show an error page at the initial request
114119
if(options.proxyTimeout) {

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

+28
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,34 @@ describe('#createProxyServer.web() using own http server', function () {
7474
http.request('http://127.0.0.1:8081', function() {}).end();
7575
});
7676

77+
it('should detect a proxyReq event and modify headers', function (done) {
78+
var proxy = httpProxy.createProxyServer({
79+
target: 'http://127.0.0.1:8080',
80+
});
81+
82+
proxy.on('proxyReq', function(proxyReq, req, res, options) {
83+
proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
84+
});
85+
86+
function requestHandler(req, res) {
87+
proxy.web(req, res);
88+
}
89+
90+
var proxyServer = http.createServer(requestHandler);
91+
92+
var source = http.createServer(function(req, res) {
93+
source.close();
94+
proxyServer.close();
95+
expect(req.headers['x-special-proxy-header']).to.eql('foobar');
96+
done();
97+
});
98+
99+
proxyServer.listen('8081');
100+
source.listen('8080');
101+
102+
http.request('http://127.0.0.1:8081', function() {}).end();
103+
});
104+
77105
it('should proxy the request and handle error via callback', function(done) {
78106
var proxy = httpProxy.createProxyServer({
79107
target: 'http://127.0.0.1:8080'

0 commit comments

Comments
 (0)