What route rule should I give for accessing multiple target endpoints if I am using RegEx with JavaScript

function joinUrlElements() {
  var re1 = new RegExp('^\\/|\\/, 'g'),
      elts = Array.prototype.slice.call(arguments);
  return elts.map(function(element){
    if ( ! element) {return '';}
    return element.replace(re1,""); }).join('/'); 

}

If I am using this code in java script and I need to call different target endpoints, ie, there are target endpoints rather than default target endpoint.

Can anyone tell the route rule that I should add. Please provide the code too

0 6 1,373
6 REPLIES 6

You don’t need a RouteRule.

If you are using that JavaScript in the target request flow to set a custom target URL, then you don’t need a route rule.

The route rule allows the ProxyEndpoint to select one TargetEndpoint from multiple availableTargetEndpoints. But it appears that in your case, there is exactly one TargetEndpoint.

I think that you have a single TargetEndpoint and you are using JavaScript logic in the target request flow to set a custom URL. In that case there is no need for a route rule.

Your next question is likely to be “when would I use multiple TargetEndpoints, and when would I want to use JavaScript to set a custom target URL?“

In the simplest case, there is just one Target in point; in this case you would use neither a route rule nor that JavaScript to set a custom URL.

Now suppose there are two distinct Target urls. In that case it makes sense to me to use two distinct TargetEndpoints, and a route rule to select between them. For three distinct yet static urls, i’d use theee distinct TargetEndpoints and RouteRules to select the appropriate one.

But if the target URL is dynamic, and needs to be constructed from multiple different elements in the request URL, then you will leave the JavaScript that you showed in order to build a URL. In that case you probably don’t need a route rule.

Now it is possible that you will have the need for a TargetEndpoint with a dynamic target URL, alongside one or more TargetEndpoints with static urls. In that case you would use a combination: multiple TargetEndpoints, and one of them would use the JavaScript you showed up above. Then, RouteRules to select between the TargetEndpoints.

Read more about RouteRules here.

One good way to think about the function of Apigee Edge is, that it maps inbound requests to outbound requests. A simple proxy that acts as a "pass-through" might do this:

inbound outbound
GET http://ORG-ENV.apigee.net/foobars GET http://mytargetserver.com/foobars
GET http://ORG-ENV.apigee.net/foobars/1234 GET http://mytargetserver.com/foobars/1234

That's a "pass-through". Basically Apigee Edge is just invoking a backend URL that looks exactly like the inbound.

But there are more complex scenarios. Apigee Edge is a smart, configurable proxy, so it can do things like, extract elements of the inbound path and graft them into a new outbound path & query string. For example:

inbound outbound
GET http://ORG-ENV.apigee.net/foobars GET http://mytargetserver.com/foobars?action=list
GET http://ORG-ENV.apigee.net/foobars/1234 GET http://mytargetserver.com/foobars?id=1234

Your scenario is a little different. It sounds to me that you want Apigee Edge to wrap multiple backend services into a single interface. Like this:

inbound outbound
GET http://ORG-ENV.apigee.net/redmine GET https://apibaas-trial.apigee.net/shebababu92/sandbox/redmine
GET http://ORG-ENV.apigee.net/redmine/uploads GET https://apibaas-trial.apigee.net/shebababu92/sandbox/redmine/uploads
GET http://ORG-ENV.apigee.net/redmine/projects GET https://apibaas-trial.apigee.net/shebababu92/sandbox/redmine/projects
GET http://ORG-ENV.apigee.net/teamforge GET https://jsonplaceholder.typicode.com
GET http://ORG-ENV.apigee.net/teamforge/projects GET https://jsonplaceholder.typicode.com/projects

And Yes, Apigee Edge can do that too.

One way to do this in Apigee Edge is to use multiple RouteRules.

<ProxyEndpoint name="default">
    <Description/>
    <FaultRules/>
    <PreFlow name="PreFlow">
        <Request>
            <Step>
                <Name>JavaScript-1</Name>
            </Step>
        </Request>
        <Response/>
    </PreFlow>
    <PostFlow name="PostFlow">
        <Request/>
        <Response/>
    </PostFlow>
    <Flows>
      <!-- these flows "whitelist" inbound requests -->
      <Flow name="f1">
        <Condition>(proxy.pathsuffix MatchesPath "/redmine") and (request.verb = "GET")</Condition>
      </Flow>
      <Flow name="f2">
        <Condition>(proxy.pathsuffix MatchesPath "/teamforge") and (request.verb = "GET")</Condition>
      </Flow>
      <!-- all other requests fall through here, get an "invalid request" response -->
      <Flow name="default">
        <Request>
          <Step><Name>RaiseFault-InvalidRequest</Name></Step>
        </Request>
      </Flow>
    </Flows>
    <HTTPProxyConnection>
        <BasePath>sheba-babu</BasePath>
        <Properties/>
        <VirtualHost>secure</VirtualHost>
    </HTTPProxyConnection>
    <RouteRule name="MyRoute0">
        <Condition>proxy.pathsuffix MatchesPath "/redmine"</condition>
        <TargetEndpoint>redmine</TargetEndpoint>
    </RouteRule>
    <RouteRule name="MyRoute1">
        <Condition>proxy.pathsuffix MatchesPath "/teamforge"</condition>
        <TargetEndpoint>teamforge</TargetEndpoint>
    </RouteRule>
    <RouteRule name="MyRoute2"/> <!-- default: no backend -->
</ProxyEndpoint>
    

This presumes that you have 2 targets defined in your API Proxy, named redmine and teamforge. The former would have

    <HTTPTargetConnection>
        <Properties/>
        <URL>https://apibaas-trial.apigee.net/shebababu92/sandbox/redmine/</URL>
    </HTTPTargetConnection>
    

...and the latter would have

    <HTTPTargetConnection>
        <Properties/>
        <URL>https://jsonplaceholder.typicode.com/</URL>
    </HTTPTargetConnection>
    

I am new to apigee. Please include the code also if possible.

I got this code

function joinUrlElements(){var re1 =newRegExp('^\\/|\\/, 'g'),
      elts = Array.prototype.slice.call(arguments);
  return elts.map(function(element){
    if ( ! element) {return '';}
    return element.replace(re1,""); }).join('/');}

from @Dino .But I don't have idea of how to proceed. Whether I can use in this case or not?

Wile hitting this "https://apibaas-trial.apigee.net/redmine" I am getting the response like "{"errorcode":"protocol.http.Response405WithoutAllowHeader" and for "https://apibaas-trial.apigee.net/redmine/shebababu92/sandbox/redmine" the response is "{"error":"organization_application_not_found","timestamp":1513816361104,"duration":0,"error_description":"Could not find application for redmine/shebababu92 from URI: redmine/shebababu92/sandbox/redmine","exception":"org.apache.usergrid.rest.exceptions.OrganizationApplicationNotFoundException"}"

I think you have some incorrect URLs there.

I think you want

https://apibaas-trial.apigee.net/shebababu92/sandbox/redmine