4

I'm trying to serve my html, css and js with akka http:

path("") {
  get {
    getFromResourceDirectory("home")
    complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, home.render().toString()))
  }
}

home.render() is a twirl template that renders:

<html>
    <head>
        <link rel="stylesheet" href="home.css"> 
        <script src="home.js"></script>
    </head>
    <body>
        ...
    </body>
</html>

The html loads without problem except for the css and js files, this is my dir structure:

enter image description here and this error in chrome:

GET http://localhost:8080/home.js net::ERR_ABORTED
localhost/:5 GET http://localhost:8080/home.css net::ERR_ABORTED
CommonSenseCode
  • 23,522
  • 33
  • 131
  • 186

1 Answers1

2

In my project I solved it with getFromResource(path) method and additional route for resources:

val resourcePrefix = "pages"

get {
    pathSingleSlash {
      getFromResource("home.html")
    } ~
    path(resourcePrefix / Remaining) { resource =>
      getFromResource(resource)
    }
}

In your template file you would refer to resources like this:

<head>
    <link rel="stylesheet" href="/pages/home.css"> 
    <script src="/pages/home.js"></script>
</head>
tkachuko
  • 1,956
  • 1
  • 13
  • 20