2
我想使用akka-http像http服務器(例如tomcat或nginx服務器)。
這個簡單的代碼可以從網頁瀏覽器加載html源代碼,但無法加載鏈接到html文件的其他源代碼。Akka-Http加載css&js資源
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import scala.io.StdIn
object MainRunner extends App {
implicit val system = ActorSystem("mySystem")
implicit val materializer = ActorMaterializer()
implicit val ec = system.dispatcher
val staticResources =
get {
path("admin") {
getFromResource("admin/index.html")
} ~ pathPrefix("admin") {
getFromResourceDirectory("admin")
}
}
val bindingFuture = Http().bindAndHandle(staticResources, "localhost", 8080)
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
StdIn.readLine() // let it run until user presses return
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done
}
這是我的HTML文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<h1>Admin area</h1>
</body>
</html>
怎樣才能解決這個問題呢?