diff --git a/README.md b/README.md
index 85dd93c..cae0f72 100644
--- a/README.md
+++ b/README.md
@@ -370,6 +370,8 @@ setTimeout(function() {
[register](#register)
[unregister](#unregister)
[notFound](#notFound)
+[onRequest](#onRequest)
+[onResponse](#onResponse)
[close](#close)
@@ -468,6 +470,44 @@ __Arguments__
---------------------------------------
+
+#### Redbird##onRequest(callback)
+
+ Gives Redbird a callback function with four parameters, the proxy, the HTTP request/response and options objects, respectively, which will be called when a proxy route is executed. The default is
+```javascript
+ function (proxy, request, response, options) {
+ }
+```
+.
+
+__Arguments__
+
+```javascript
+ src {Function(proxy, request, response, options)} The callback which will be
+ called before the proxy redirects the request to the target
+```
+
+---------------------------------------
+
+
+#### Redbird##onResponse(callback)
+
+ Gives Redbird a callback function with one parameter, the HTTP response object, respectively, which will be called when a proxy route was executed. The default is
+```javascript
+ function (response) {
+ }
+```
+.
+
+__Arguments__
+
+```javascript
+ src {Function(response)} The callback which will be
+ called after the proxy server received a response from the target server
+```
+
+---------------------------------------
+
#### Redbird##close()
diff --git a/lib/proxy.js b/lib/proxy.js
index 2a2b41f..c8e38f2 100644
--- a/lib/proxy.js
+++ b/lib/proxy.js
@@ -85,12 +85,16 @@ function ReverseProxy(opts) {
*/
});
- proxy.on('proxyReq', function (p, req) {
+ proxy.on('proxyReq', function (p, req, res, options) {
+ onRequestCallback(p,req,res,options);
+
if (req.host != null) {
p.setHeader('host', req.host);
}
});
-
+ proxy.on('proxyRes', function (res) {
+ onResponseCallback(res);
+ });
//
// Support NTLM auth
//
@@ -635,13 +639,36 @@ var respondNotFound = function (req, res) {
res.end();
};
+var onRequestCallback = function (req, res) {
+}
+
+var onResponseCallback = function (req, res) {
+}
+
ReverseProxy.prototype.notFound = function (callback) {
+ respondNotFound = testIsFunction(callback);
+};
+
+ReverseProxy.prototype.onRequest = function (callback) {
+ onRequestCallback = testIsFunction(callback);
+};
+
+ReverseProxy.prototype.onResponse = function (callback) {
+ onResponseCallback = testIsFunction(callback);
+};
+
+//
+// Checks if
+// argument is a function and returns it
+// or
+// throws an exception
+//
+function testIsFunction(callback) {
if (typeof callback == "function")
- respondNotFound = callback;
+ return callback;
else
throw Error('notFound callback is not a function');
-};
-
+}
//
// Redirect to the HTTPS proxy
//