Skip to content

Commit

Permalink
add support for modify response
Browse files Browse the repository at this point in the history
  • Loading branch information
guoxiangyang authored and indexzero committed Apr 20, 2018
1 parent 2c44039 commit e5c02b8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

```
Expand All @@ -395,6 +396,7 @@ proxyServer.listen(8015);
};
```

**NOTE:**
`options.ws` and `options.ssl` are optional.
`options.target` and `options.forward` cannot both be missing
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions lib/http-proxy/passes/web-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit e5c02b8

Please sign in to comment.