http-proxy
Advanced tools
Comparing version 1.1.6 to 1.2.0
@@ -112,2 +112,7 @@ var http = require('http'), | ||
// 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 | ||
@@ -114,0 +119,0 @@ // show an error page at the initial request |
{ | ||
"name" : "http-proxy", | ||
"version" : "1.1.6", | ||
"version" : "1.2.0", | ||
@@ -5,0 +5,0 @@ "repository" : { |
@@ -117,2 +117,39 @@ <p align="center"> | ||
#### 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 | ||
@@ -119,0 +156,0 @@ |
@@ -77,2 +77,30 @@ var webPasses = require('../lib/http-proxy/passes/web-incoming'), | ||
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) { | ||
@@ -79,0 +107,0 @@ var proxy = httpProxy.createProxyServer({ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
170596
2865
361