我有一个喷涂应用程序。
//src/main/resources/twirl/app/index.scala.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="css/file.css" />
</head>
<body>
...
</body>
</html>
在src/main/resources/css/file.css
我的带有样式的css文件中。
我的服务:
//src/main/scala/app/service.scala
trait MyRoute extends HttpService {
val route = pathPrefix("") {
get {
respondWithMediaType(`text/html`) {
complete {
app.html.index.render
}
}
}
} ~
pathPrefix("css") {
get {
getFromResourceDirectory("css")
}
}
class MyActor extends Actor with MyRoute {
def actorRefFactory = context
def receive = runRoute(route)
}
//main app
object Main extends App {
implicit val system = ActorSystem("app")
val service = system.actorOf(Props[MyActor], "my-service")
IO(Http) ! Http.Bind(service, "localhost", port = 8080)
}
当我运行此应用程序时,我进入了chrome控制台:Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:8080/css/file.css".
而不是file.css
内容,我得到了index.scala.html
内容。如何使用spray.io返回css
静态文件?