2016-08-31 6 views
1

私はKotlinでVert.X(をorg.jetbrains.kotlinx:vertx3-lang-kotlinライブラリ)経由で使用していますが、私は1ページのアプリケーションを構築しようとしています - 瓶に入っています。 Mavenの側ではVertXで静的コンテンツをルーティングする

、これらは私の依存関係している:私のメインクラスで

<properties>   
    ...    
    <vertx.version>3.3.2</vertx.version> 
    <kotlin.version>1.0.3</kotlin.version> 
    <main.class>Bob</main.class> 
</properties> 

<!-- Vertx Dependencies --> 
<dependency> 
    <groupId>io.vertx</groupId> 
    <artifactId>vertx-core</artifactId> 
    <version>${vertx.version}</version> 
</dependency> 
<dependency> 
    <groupId>io.vertx</groupId> 
    <artifactId>vertx-web</artifactId> 
    <version>${vertx.version}</version> 
</dependency> 

<!-- Kotlin Dependencies --> 
<dependency> 
    <groupId>org.jetbrains.kotlin</groupId> 
    <artifactId>kotlin-stdlib</artifactId> 
    <version>${kotlin.version}</version> 
</dependency> 
<dependency> 
    <groupId>org.jetbrains.kotlin</groupId> 
    <artifactId>kotlin-test</artifactId> 
    <version>${kotlin.version}</version> 
    <scope>test</scope> 
</dependency> 

<!-- Kotlin Vertx Binding Dependency --> 
<dependency> 
    <groupId>org.jetbrains.kotlinx</groupId> 
    <artifactId>vertx3-lang-kotlin</artifactId> 
    <version>[0.0.4,0.1.0)</version> 
</dependency> 

、私は次のことを持っている:

object Bob { 

    val log = LoggerFactory.getLogger(javaClass) 
    val port = 9999 

    @JvmStatic 
    fun main(args: Array<String>) { 

     DefaultVertx { 
      httpServer(port = Bob.port, block = Route { 
       GET("/") { request -> 
        headers().add("Author", "[email protected]") 
        contentType("text/html") 
        sendFile("html/index.html") 
       } 
       otherwise { 
        setStatus(404, "Resource not found") 
        body { 
         write("The requested resource was not found\n") 
        } 
       } 
      }); 
     } 


    } 

に行くindex.htmlが正常に処理されています。予想通り は、私が今のCSS/JSファイルを要求し、同様のリソースのそれぞれについて

<!doctype html> 
<html> 
<head> 
    <link rel="stylesheet" href="css/bootstrap.min.css" > 
    <link rel="stylesheet" href="css/bootstrap-theme.min.css" > 
    <script src="js/bootstrap.min.js"></script> 

をそれらを提供することができるようにしたい、ブラウザがエラー404を返します。

私は今

   GET("/css/*") { 
       request -> 
        println("Serving CSS") 
        contentType("text/css") 
        sendFile("css/${request.path()}") 
      } 

を使用してCSSにサービスを提供しようとしている。しかし、私はそれが今までそのブロックに入る見ていないことだし、それがエラー404エラーにサービスを提供し続けています。

Vert.Xで静的ファイルを提供する正しい方法は何ですかorg.jetbrains.kotlinx:vertx3-lang-kotlin library?

答えて

2

これははるかに簡単です。
あなたの例では、それは次のようになります。

router.route("/css/*").handler(StaticHandler.create()); 

しかし、実際には、パブリックフォルダおよびそのようなサーバーを/静的または/の下にすべてをかける必要があります。

router.route("/static/*").handler(StaticHandler.create()); 

それが、通常に見える私のKotlinアプリケーション以下のような:

val router = Router.router(vertx) 

router.route().handler(StaticHandler.create()) 
// Other routes here... 

あなたはStaticHandlerそのように作成すると、それはデフォルトで/resources/webrootディレクトリからサーバーになります。

+0

ここで、 'Route {...}'の中で 'router.route'をどこで呼びますか?私は明日の朝初めてそれを試してみるでしょう。 –

+1

私は元の答えでKotlinに完全な例を追加しました。楽しい! –

関連する問題