What are your link strategies for Express apps in Hosted Targets ?

robinm
New Member

The overiew promises ..."You can debug and test your app locally before deploying it and be assured that the deployed version will work exactly as it does locally". And so it mostly does - until your NodeJS application is an Express App that serves static or rendered resources as well as relative links.

Then the deployed basepath of the proxy is a problem, such as when your page may redirect to a resource such as href="https://community.apigee.com/login".
Locally this runs fine, because your server host is root, and "http://localhost:8080/login" is found.
Once in Apigee HT, the link is effectively "https://yourorg-test.apigee.net/login" ...
whereas the desired root is actually "https://yourorg-test.apigee.net/basepath/login".

  1. My current workaround is ... examine response payload and adjust links to insert the basepath.
    This is any css, html, .js and also the "location" header for redirects.
    Messy and error-prone - after every apigeetool deploy, I must add these policies.

2. Do we have access to the .htaccess file and would that even help ?

3. Are code changes preferred for my users .... including css image links ?

4. What about redirects to 3rd-party IDP pages ?

What are your recommendations and other bright ideas ?

0 6 224
6 REPLIES 6

Just to clarify is your API Proxy /login or /basepath/login

Also, what version of express is this? I wonder if for example, this relates to express routes config?

Hi Dane.
The proxy has to be /basepath/login.
Only one proxy per Apigee Org and environment can live at root. (yourorg-environ/?????)

Express 4.17.12 and works fine running locally as explained.
The basepath injection results when proxied by Apigee, externally to Express routes.
( Unless I am missing something very obvious 🙂 ).

Just to check, your example uses an absolute path of "/login". Would you not want a relative path of "./login" ?

Changing the coded links to relative from root "./login" does not change anything.
The link still ends up going to "https://myorg-test.apigee.net/login" ... i.e. it is not directed to the HT proxy due to missing the basepath.

when you say your page my redirect to a resource ... do you mean it's returning a 302 redirect to the client

eg

1. client (eg browser) comes to abc.apigee.net/basepath/xyz

2. this returns a 302 with a location header of "./login" ?

3. i would expect client then goes to abc.apigee.net/basepath/login

No. Redirect excludes the basepath.
Trace can show header, yes, but location attribute has no basepath.


Why ? Well because the HostedTarget Express app does not know it lives behind a basepath. Hence my earlier question about the .htaccess file equivalent.

Remember this is a web application, serving static resources and HTML pages with embedded links. By the time the call gets to the HT instance, it is basepath unaware. It does not know where it lives.
The HT instance is called as "http://localhost/user" as shown here.

11059-ht-nobasepath.jpg


These pages are rendered dynamically (PUG) and sent to the caller, for example `a href="https://community.apigee.com/login"` or `a href="https://community.apigee.com/user"`.
Your browser will interpret these according to the hostname, which excludes the Apigee basepath.
I.E: I visit home page via "https://myorg-test.apigee.net/basepath".
The index page is returned but the CSS and images are not found.
( e.g. they live on /basepath/public/imagename.jpg)
The raw index page is returned and inside is a link to "/user".
If I click that link, where do you think the browser will try to go ?

The Express app is called without passing the basepath.