I'm trying to serve an Angular 7 application using path location strategy in a Wildfly application server.
I managed to add the app as static resources, using a static file handler. So it's getting delivered correctly when an exact path is requested. E.g. requesting http://my-wildfly-server/myApp/
sends the index.html
of my app.
Since I'm using path location strategy the index.html
file should be sent to the client for sublinks too. E.g. requesting http://my-wildfly-server/myApp/my/route
should send the index.html
of my app as well.
This leads me to my actual question: Is it possible to configure Wildfly or the Undertow subsystem with some kind of filter which is equal to nginx's try_file
rule? In other words: Serve the correct file, if it exists for a specific request e.g. http://my-wildfly/myApp/styles.css
or otherwise fall back to the index.html
and let Angular take care of the route?
If possible I'd like to avoid bundling my WebApp into a WAR-file or change it to use hash location strategy...
Just for the sake of completeness I post here my other solution to achieve my original goal. :)
As I described in the comments of @Tyrone's answer, I ended up bundling the compiled Angular application into a WAR-File and adding the following files to the WEB-INF
folder:
jboss-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web xmlns="http://www.jboss.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd">
<context-root>/</context-root>
</jboss-web>
I wanted my web application to be hosted in the root of the web server. Therefore the context-root
is /
...
undertow-handlers.conf
path-prefix('/api') -> done
regex('^(.*\/)(.*\.[a-zA-Z0-9]{2,5})$') -> done
path-prefix('/en') -> rewrite('/en')
path-prefix('/de') -> rewrite('/de')
path-prefix('/') -> rewrite('/en')
All my /api
calls should go directly to the corresponding route (configured in the API WAR-File). The second line with the regex ensures, that every call to something which is most probably a file, goes through directly as well... Since I have a multi-language application I need to redirect the different languages to the index.html file of the correct folder (line 3 and 4). And last but not least, if the root of the web server is hit I want to redirect to the default language, which in my case is English (line 5).
Hope that helps.