3
votes

I'm trying to serve angular2(rc3) app from akka-http(scala rest framework). It doesn't work because of routing. When requested, spray tries to find /my/endpoint/declared/in/angular as normal resource. How should I do it?

With simple static webpages, without routing(dynamic links) it is pretty easy:

path("app") {
  getFromResource("my-app/index.html")
} ~ getFromResourceDirectory("my-app")

I've tried to serve index.html every time when requested resource is not found:

path("app") {
  getFromResource("my-app/index.html")
} ~ getFromResourceDirectory("my-app") ~ getFromResource("my-app/index.html")

but it breaks relative links( when I ask for /app/my/endpoint I get index.html which tries to download styles.css from /app/my/endpoint/styles.css instead of /app/styles.css)

I would like to know how it is done in other http servers.

1

1 Answers

1
votes

I solved the problem. It may not be optimal but works pretty well.

In the spray config you put:

path("ui") { //the same prefix must be set as base href in index.html
  getFromResource("abtest-ui/index.html")
} ~ pathPrefix("ui") {
getFromResourceDirectory("abtest-ui") ~
  getFromResource("abtest-ui/index.html")
}

And in the index.html:

<base href="/ui/">

To summarize:

  • request to /ui/ serves index.html
  • request to /ui/someResourceFile.css serves this resource
  • request to /ui/AnythingThat/IsNotA/ResourceFile serves index.html and angular routing handles it