我尝试使用 Route.static 函数来提供静态文件,但它不起作用。我在工作目录中尝试了各种组合和文件夹。现在我在resources/static/css下有一个css,在resources/static下有一个index.html。如果我写以下内容,我不会得到任何服务:
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
val client = HttpClient(Apache) {}
routing {
static("root") {
files("css")
default("index.html")
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我可以根据这个答案:Ktor - Static contentrouting,写:
resource("/", "index.html")
resource("*", "index.html")
Run Code Online (Sandbox Code Playgroud)
然后我会在resources/static下得到index.html。然而我无法得到任何其他东西。我在这里做错了什么?
参考: https: //ktor.io/servers/features/static-content.html#specifying-files-and-folders
小智 1
假设您的项目位于 C:\ktor-project 文件夹中。如果您希望您的 Ktor 服务器使用以下 URL 并返回以下相应文件的内容,请使用底部的代码。
http://localhost:8080/root/main.css -> C:\ktor-project\css\main.css http://localhost:8080/root -> C:\ktor-project\index.html http: //localhost:8080/root/styles.css -> C:\ktor-project\resources\static-resources\styles.css
static("/root") {
files("css")
default("index.html")
resources("static-resources")
}
Run Code Online (Sandbox Code Playgroud)
如果给定的 styles.css 文件是由 Gradle 构建构建的,那么 resources("static-resources") 行可能是比 files("css") 行更好的选择。大多数时候您只需要其中之一。
| 归档时间: |
|
| 查看次数: |
3024 次 |
| 最近记录: |