Skip to content

Commit e5c02b8

Browse files
guoxiangyangindexzero
authored andcommitted
add support for modify response
1 parent 2c44039 commit e5c02b8

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ proxyServer.listen(8015);
377377
* **timeout**: timeout (in millis) for incoming requests
378378
* **followRedirects**: true/false, Default: false - specify whether you want to follow redirects
379379
* **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
380+
* **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
380381
* **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:
381382
382383
```
@@ -395,6 +396,7 @@ proxyServer.listen(8015);
395396
396397
};
397398
```
399+
398400
**NOTE:**
399401
`options.ws` and `options.ssl` are optional.
400402
`options.target` and `options.forward` cannot both be missing
@@ -485,6 +487,30 @@ proxy.close();
485487

486488
### Miscellaneous
487489

490+
### Modify response
491+
492+
```
493+
494+
var option = {
495+
target: target,
496+
modifyResponse : true
497+
};
498+
proxy.on('proxyRes', function (proxyRes, req, res) {
499+
var body = new Buffer('');
500+
proxyRes.on('data', function (data) {
501+
body = Buffer.concat([body, data]);
502+
});
503+
proxyRes.on('end', function () {
504+
body = body.toString();
505+
console.log("res from proxied server:", body);
506+
res.end("my response to cli");
507+
});
508+
});
509+
proxy.web(req, res, option);
510+
511+
512+
```
513+
488514
#### ProxyTable API
489515

490516
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.

lib/http-proxy/passes/web-incoming.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ module.exports = {
181181
proxyRes.on('end', function () {
182182
if (server) server.emit('end', req, res, proxyRes);
183183
});
184-
// We do this separately since we are also checking for res.finished
185-
if (!options.selfHandleResponse) proxyRes.pipe(res);
184+
// We pipe to the response unless its expected to be handled by the user
185+
if (!options.selfHandleResponse && !options.modifyResponse) proxyRes.pipe(res);
186186
} else {
187187
if (server) server.emit('end', req, res, proxyRes);
188188
}

0 commit comments

Comments
 (0)