@mocks-server/plugin-proxy v4.0.0
Mocks Server Plugin Proxy
Plugin for Mocks Server that provides a variant handler that proxy requests to another host and pass response back to original caller.
It uses the express-http-proxy package under the hood, and supports all of its options.
Important: From v3.0.0, this plugin includes two route handlers:
proxyandproxy-v4. This was made to allow Mocks Server v3 users to progressively adapt their code to Mocks Server v4 without breaking changes. It is strongly recommended to use theproxy-v4handler. In the next major release, backward compatibilty will be removed and theproxyhandler will be replaced byproxy-v4.
Usage of proxy-v4 handler
This plugin is included in the main distribution of the Mocks Server project, so you can also read the official documentation website.
Proxy routes
If you want a route variant to use the proxy-v4 handler, define its handler property as "proxy-v4". Use the host property to set the host for the route, and the options property to set any of the express-http-proxy options.
module.exports = [
{
id: "proxy-all",
url: "*",
method: ["GET", "POST", "PATCH", "PUT"],
variants: [
{
id: "proxy-to-google",
type: "proxy-v4", // This route variant will use the "proxy" handler from this plugin
options: {
host: "https://www.google.com", // proxy host
options: {}, // Options for express-http-proxy
},
},
],
},
];Options
Here are listed the specific properties that can be defined in a proxy-v4 route variant. They must be defined in the options property of the variant:
host(String|Function): The proxy host. Equivalent to theexpress-http-proxyhostoption, so it can also be a function.options(Object): Object containing any of the options supported by theexpress-http-proxypackage. Some of them are:- filter (Function):
filteroption forexpress-http-proxy. - userResDecorator (Function):
userResDecoratoroption forexpress-http-proxy. - ... all other
express-http-proxyoptions are also supported.
- filter (Function):
Tip: Note that the variant
delayoption is still valid for routes handled by this plugin, so you can use it to simulate that host responses are slow.
Usage of proxy handler
Proxy routes
If you want a Mocks Server v3 route variant to use the proxy handler, define its type property as "proxy". Use the host property to set the host for the route, and the options property to set any of the express-http-proxy options.
module.exports = [
{
id: "proxy-all",
url: "*",
method: ["GET", "POST", "PATCH", "PUT"],
variants: [
{
id: "proxy-to-google",
type: "proxy", // This route variant will use the "proxy" handler from this plugin
host: "https://www.google.com", // proxy host
options: {}, // Options for express-http-proxy
},
],
},
];Route variant options
Mocks server common properties to all route handlers are in cursive. Specific properties of this plugin are in bold:
host(String|Function): The proxy host. Equivalent to theexpress-http-proxyhostoption, so it can also be a function.options(Object): Object containing any of the options supported by theexpress-http-proxypackage. Some of them are:- filter (Function):
filteroption forexpress-http-proxy. - userResDecorator (Function):
userResDecoratoroption forexpress-http-proxy. - ... all other
express-http-proxyoptions are also supported.
- filter (Function):
Tip: Note that the
delayoption is still valid for routes handled by this plugin, so you can use it to simulate that host responses are slow.
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago