Proxy Router
Thiago da Rosa de Bustamante edited this page Jan 19, 2018
·
2 revisions
A Router is a function that receives the request object and must return a string value (or a Promise<string>
) to inform the target destination for this proxy.
Each router must be defined on its own .js file.
Example:
/**
* Where request is the original request received by the gateway.
*/
module.exports = function (request) {
return req.query.version == '2'? 'http://myapiversiontwo/':'http://myapiversionone/';
};
or, using Promises:
module.exports = function (request) {
return new Promise((resolve, reject) => {
setTimeout(function(){resolve(req.query.version == '2'? 'http://myapiversiontwo/':'http://myapiversionone/');}, 10);
};
};
You can configure a proxy router middleware through:
- Admin Rest API:
POST /midleware/proxy/router
- SDK:
sdk.middleware.addProxyRouter(name, fileName);
- CLI:
treeGatewayConfig middleware proxyRouter -a <name> ./filename.js
Tree Gateway provide some router middlewares for common tasks already included in its distribution. Check the list here.