Tree Gateway - Routers

We are assuming in these examples, that our test gateway will be running at 'http://gateway.address.com'.

Query Router

Query Router --- name: TestQueryAPI version: 1.0.0 path: "/queryTest" proxy: target: router: middleware: name: query options: param: apiVersion defaultTarget: http://httpbin.org destinations: - target: http://httpbin.org value: '1' - target: http://httpbin.org/anything value: '2'

This configuration will make requests to http://gateway.address.com/queryTest?apiVersion=1 be routed to http://httpbin.org and requests to http://gateway.address.com/queryTest?apiVersion=2 be routed to http://httpbin.org/anything.

If no parameter is provided, the request will be routed to http://httpbin.org.

Header Router

Header Router --- name: TestHeaderAPI version: 1.0.0 path: "/headerTest" proxy: target: router: middleware: name: header options: name: host defaultTarget: http://httpbin.org destinations: - target: http://httpbin.org value: api.address.com - target: http://httpbin.org/anything value: docs.address.com

This sample is very similar to the previous one. The difference is that it uses the header 'host' to choose between the targets.

Traffic Split Router

Traffic Split --- name: TestTrafficSplitAPI version: 1.0.0 path: "/trafficSplit" proxy: target: router: middleware: name: trafficSplit options: destinations: - target: http://httpbin.org weight: 75 - target: http://httpbin.org/anything weight: 25

This configuration will route requests to http://httpbin.org and to http://httpbin.org/anything. The first target will receive a bigger load (3 times greater than the second one).

Load Balancer

Load Balancer --- name: TestLoadBalancerAPI version: 1.0.0 path: "/loadBalancer" proxy: target: router: middleware: name: loadBalancer options: destinations: - target: http://httpbin.org weight: 75 healthCheck: http://httpbin.org/get - target: http://httpbin.org/anything weight: 25 healthCheck: http://httpbin.org/anything/get database: checkInterval: 5 minutes strategy: weight healthCheckOptions: checkInterval: 5 seconds failCount: 3 waitTimeout: 5 seconds

This configuration will route requests to http://httpbin.org and to http://httpbin.org/anything. The first target will receive a bigger load (3 times greater than the second one). It will use a database to be able to change the configuration dynamically and a health check will be performed to avoid traffic redirection to unavailable servers.

Round-Robin Load Balancer

Round-Robin Load Balancer --- name: TestLoadBalancerAPI version: 1.0.0 path: "/loadBalancer" proxy: target: router: middleware: name: loadBalancer options: destinations: - target: http://httpbin.org healthCheck: http://httpbin.org/get - target: http://httpbin.org/anything healthCheck: http://httpbin.org/anything/get database: key: 'my-table-name' checkInterval: 5 minutes strategy: round-robin

This sample is similar to the previous one. The difference is that it uses a round-robin balance strategy.

Custom Router

Custom Router --- name: TestCustomRouterAPI version: 1.0.0 path: "/customrouter" proxy: target: router: middleware: name: myCustomRouter /** * Where request is the original request received by the gateway. */ module.exports = function (request) { return req.query.version == '2'? 'http://myapiversiontwo/':'http://myapiversionone/'; };

You can define any custom middleware to route the requests to your APIs. Your router middleware can also return a promise if you need an async behavior.