在 VertX 中路由静态内容
Routing static content in VertX
我在 Kotlin 中使用 Vert.X(通过 org.jetbrains.kotlinx:vertx3-lang-kotlin 库),我正在尝试构建一个独立于 jar 中的单页应用程序。
在 Maven 方面,这些是我的依赖项:
<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>
在我的主要 class 中,我有以下内容:
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", "Re@PeR")
contentType("text/html")
sendFile("html/index.html")
}
otherwise {
setStatus(404, "Resource not found")
body {
write("The requested resource was not found\n")
}
}
});
}
}
前往 localhost:9999
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>
对于每个资源,浏览器 returns 一个预期的错误 404。
我现在正在尝试使用
服务 CSS
GET("/css/*") {
request ->
println("Serving CSS")
contentType("text/css")
sendFile("css/${request.path()}")
}
但我没有看到它进入那个块,它继续提供错误 404。
使用 org.jetbrains.kotlinx:vertx3-lang-kotlin 库在 Vert.X 中提供静态文件的正确方法是什么?
简单多了。
在您的示例中,它可能是:
router.route("/css/*").handler(StaticHandler.create());
但实际上,您应该将所有内容放在 /static 或 /public 文件夹下并像这样服务器:
router.route("/static/*").handler(StaticHandler.create());
我的 Kotlin 应用程序通常看起来像:
val router = Router.router(vertx)
router.route().handler(StaticHandler.create())
// Other routes here...
当您以这种方式创建 StaticHandler 时,默认情况下它将从 /resources/webroot
目录提供服务器。
我在 Kotlin 中使用 Vert.X(通过 org.jetbrains.kotlinx:vertx3-lang-kotlin 库),我正在尝试构建一个独立于 jar 中的单页应用程序。
在 Maven 方面,这些是我的依赖项:
<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>
在我的主要 class 中,我有以下内容:
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", "Re@PeR")
contentType("text/html")
sendFile("html/index.html")
}
otherwise {
setStatus(404, "Resource not found")
body {
write("The requested resource was not found\n")
}
}
});
}
}
前往 localhost:9999
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>
对于每个资源,浏览器 returns 一个预期的错误 404。
我现在正在尝试使用
服务 CSS GET("/css/*") {
request ->
println("Serving CSS")
contentType("text/css")
sendFile("css/${request.path()}")
}
但我没有看到它进入那个块,它继续提供错误 404。
使用 org.jetbrains.kotlinx:vertx3-lang-kotlin 库在 Vert.X 中提供静态文件的正确方法是什么?
简单多了。
在您的示例中,它可能是:
router.route("/css/*").handler(StaticHandler.create());
但实际上,您应该将所有内容放在 /static 或 /public 文件夹下并像这样服务器:
router.route("/static/*").handler(StaticHandler.create());
我的 Kotlin 应用程序通常看起来像:
val router = Router.router(vertx)
router.route().handler(StaticHandler.create())
// Other routes here...
当您以这种方式创建 StaticHandler 时,默认情况下它将从 /resources/webroot
目录提供服务器。