2. Testing & Conclusion
From Postman or using cURL, invoke the Router_MPG service and you should see different responses based on the content sent.
For example, if there is a request for “name”, the response will bring back profile details:
If there is a request for “accounts”, the response will bring back account details:
If there is a request for “beneficiaries”, the response will bring back beneficiary details:
As you can see, the router.js is similar to the content based routing from a stylesheet IBM tech note: https://www.ibm.com/support/pages/content-based-routing-stylesheet-using-routing-url-method-websphere-datapower-soa-appliance
The <dp-set-target> and <dp:set-variable name=”‘var://service/routing-url'” value=”$targetURL”/> extension functions are similar to “setVar(‘var://service/routing-url’)” found in the router.js script as shown below.
The gatewayscript apis used in the router.js script may be used to route requests to multiple backends based on different content, such as:
- the inbound request URI:
- a certain HTTP Header:
- var hm = require(‘header-metadata’);
- query paramters:
- var query = new String(service.URI);
var querys = query.split(“=”);
More GatewayScript APIs may be found in the IBM DataPower Knowledge Center under the GatewayScript APIs section.
More service variables may be found in the IBM DataPower Knowledge Center under the Service Variables section.
var hm = require('header-metadata');
var service = require('service-metadata');
//Read input
session.input.readAsBuffers(function (error, input) {
if (error) {
// handle error
session.output.write (error.errorMessage);
}
else {
if (input == '') {
session.output.write("Please enter name, accounts, or beneficiaries.");
service.mpgw.skipBackside = true;
}
//If input contains "name", route traffic to /profile endpoint.
if (input.indexOf('name') > -1) {
service.setVar('var://service/routing-url', 'http://localhost:2000/profile');
}
//If input contains "accounts", route traffic to /accounts endpoint.
if (input.indexOf('accounts') > -1) {
service.setVar('var://service/routing-url', 'http://localhost:2000/accounts');
}
//If input contains "beneficiaries", route traffic to /beneficiaries endpoint.
if (input.indexOf('beneficiaries') > -1) {
service.setVar('var://service/routing-url', 'http://localhost:2000/beneficiaries');
}
}
});