2016-08-31 2 views
1

Kotlin에서 Vert.X (을 org.jetbrains.kotlinx : vertx3-lang-kotlin 라이브러리) 통해 사용하고 있으며 자체 페이지 하나의 애플리케이션을 작성하려고합니다. - 항아리에 담겨있다. 받는다는 측면에서 VertX의 정적 컨텐츠 라우팅

이 내 의존성은 다음과 같습니다

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") 
        } 
       } 
      }); 
     } 


    } 
:

<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> 

내 메인 클래스에서, 나는 다음있어

로갑니다.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()); 

그것은 내 코 틀린 응용 프로그램을 같은 :

val router = Router.router(vertx) 

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

당신이 그런 식으로 StaticHandler을 만들 때, 그것은 것입니다 서버 기본적으로 /resources/webroot 디렉토리에서.

+0

어디에서'router {route} '를 호출할까요? 내일 아침에 먼저 해 볼게. –

+1

나는 Kotlin의 전체 예제를 원래의 대답으로 추가했습니다. 즐겨! –

관련 문제