Skip to content

[api] Add event-based ability to modify pre-flight proxy requests. #673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,43 @@ console.log("listening on port 5050")
server.listen(5050);
```

#### Setup a stand-alone proxy server with proxy request header re-writing
This example shows how you can proxy a request using your own HTTP server that
modifies the outgoing proxy request by adding a special header.

```js
var http = require('http'),
httpProxy = require('http-proxy');

//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createProxyServer({});

// To modify the proxy connection before data is sent, you can listen
// for the 'proxyReq' event. When the event is fired, you will receive
// the following arguments:
// (http.ClientRequest proxyReq, http.IncomingMessage req,
// http.ServerResponse res, Object options). This mechanism is useful when
// you need to modify the proxy request before the proxy connection
// is made to the target.
//
proxy.on('proxyReq', function(proxyReq, req, res, options) {
proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
});

var server = require('http').createServer(function(req, res) {
// You can define here your custom logic to handle the request
// and then proxy the request.
proxy.web(req, res, {
target: 'http://127.0.0.1:5060'
});
});

console.log("listening on port 5050")
server.listen(5050);
```

#### Setup a stand-alone proxy server with latency

```js
Expand Down
5 changes: 5 additions & 0 deletions lib/http-proxy/passes/web-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ web_o = Object.keys(web_o).map(function(pass) {
common.setupOutgoing(options.ssl || {}, options, req)
);

// Enable developers to modify the proxyReq before headers are sent
proxyReq.on('socket', function(socket) {
if(server) { server.emit('proxyReq', proxyReq, req, res, options); }
});

// allow outgoing socket to timeout so that we could
// show an error page at the initial request
if(options.proxyTimeout) {
Expand Down
28 changes: 28 additions & 0 deletions test/lib-http-proxy-passes-web-incoming-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,34 @@ describe('#createProxyServer.web() using own http server', function () {
http.request('http://127.0.0.1:8081', function() {}).end();
});

it('should detect a proxyReq event and modify headers', function (done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080',
});

proxy.on('proxyReq', function(proxyReq, req, res, options) {
proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
});

function requestHandler(req, res) {
proxy.web(req, res);
}

var proxyServer = http.createServer(requestHandler);

var source = http.createServer(function(req, res) {
source.close();
proxyServer.close();
expect(req.headers['x-special-proxy-header']).to.eql('foobar');
done();
});

proxyServer.listen('8081');
source.listen('8080');

http.request('http://127.0.0.1:8081', function() {}).end();
});

it('should proxy the request and handle error via callback', function(done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080'
Expand Down