add support for modify response

This commit is contained in:
guoxiangyang 2017-06-08 00:45:00 +08:00 committed by Charlie Robbins
parent 2c44039a7c
commit e5c02b8a8a
2 changed files with 28 additions and 2 deletions

View File

@ -377,6 +377,7 @@ proxyServer.listen(8015);
* **timeout**: timeout (in millis) for incoming requests
* **followRedirects**: true/false, Default: false - specify whether you want to follow redirects
* **selfHandleRequest** true/false, if set to true, none of the webOutgoing passes are called and its your responsibility ro appropriately return the response by listening and acting on the `proxyRes` event
* **modifyResponse**: do not pipe proxyRes to res, so you can respond to client with your own response. It still goes through all out going web passes unlike selfHandleRequest
* **buffer**: stream of data to send as the request body. Maybe you have some middleware that consumes the request stream before proxying it on e.g. If you read the body of a request into a field called 'req.rawbody' you could restream this field in the buffer option:
```
@ -395,6 +396,7 @@ proxyServer.listen(8015);
};
```
**NOTE:**
`options.ws` and `options.ssl` are optional.
`options.target` and `options.forward` cannot both be missing
@ -485,6 +487,30 @@ proxy.close();
### Miscellaneous
### Modify response
```
var option = {
target: target,
modifyResponse : true
};
proxy.on('proxyRes', function (proxyRes, req, res) {
var body = new Buffer('');
proxyRes.on('data', function (data) {
body = Buffer.concat([body, data]);
});
proxyRes.on('end', function () {
body = body.toString();
console.log("res from proxied server:", body);
res.end("my response to cli");
});
});
proxy.web(req, res, option);
```
#### ProxyTable API
A proxy table API is available through this add-on [module](https://github.com/donasaur/http-proxy-rules), which lets you define a set of rules to translate matching routes to target routes that the reverse proxy will talk to.

View File

@ -181,8 +181,8 @@ module.exports = {
proxyRes.on('end', function () {
if (server) server.emit('end', req, res, proxyRes);
});
// We do this separately since we are also checking for res.finished
if (!options.selfHandleResponse) proxyRes.pipe(res);
// We pipe to the response unless its expected to be handled by the user
if (!options.selfHandleResponse && !options.modifyResponse) proxyRes.pipe(res);
} else {
if (server) server.emit('end', req, res, proxyRes);
}