mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
add support for modify response
This commit is contained in:
parent
2c44039a7c
commit
e5c02b8a8a
26
README.md
26
README.md
@ -377,6 +377,7 @@ proxyServer.listen(8015);
|
|||||||
* **timeout**: timeout (in millis) for incoming requests
|
* **timeout**: timeout (in millis) for incoming requests
|
||||||
* **followRedirects**: true/false, Default: false - specify whether you want to follow redirects
|
* **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
|
* **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:
|
* **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:**
|
**NOTE:**
|
||||||
`options.ws` and `options.ssl` are optional.
|
`options.ws` and `options.ssl` are optional.
|
||||||
`options.target` and `options.forward` cannot both be missing
|
`options.target` and `options.forward` cannot both be missing
|
||||||
@ -485,6 +487,30 @@ proxy.close();
|
|||||||
|
|
||||||
### Miscellaneous
|
### 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
|
#### 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.
|
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.
|
||||||
|
|||||||
@ -181,8 +181,8 @@ module.exports = {
|
|||||||
proxyRes.on('end', function () {
|
proxyRes.on('end', function () {
|
||||||
if (server) server.emit('end', req, res, proxyRes);
|
if (server) server.emit('end', req, res, proxyRes);
|
||||||
});
|
});
|
||||||
// We do this separately since we are also checking for res.finished
|
// We pipe to the response unless its expected to be handled by the user
|
||||||
if (!options.selfHandleResponse) proxyRes.pipe(res);
|
if (!options.selfHandleResponse && !options.modifyResponse) proxyRes.pipe(res);
|
||||||
} else {
|
} else {
|
||||||
if (server) server.emit('end', req, res, proxyRes);
|
if (server) server.emit('end', req, res, proxyRes);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user