首页 > 解决方案 > 带有 ktor 的静态目录 - 不在“mysite.com/static/...”下

问题描述

我通读了Serving Static Content并且脑子有问题。

我有一个文件夹/resources/static。它里面有静态的东西。其中一些是 index.html、favicon.ico、css 文件夹、js 文件夹等。

我希望它显示为https://example.org/favicon.icohttps://example.org/(默认为 index.html)等。

文档中的所有示例都适用于显示为https://example.org/static/index.html的站点

我没有得到“静态”、“资源”、“资源”、“文件”之间的区别

routing {
    static("static") {
        files("css")
        files("js")
        file("image.png")
        file("random.txt", "image.png")
        default("index.html")
    }
}

标签: ktor

解决方案


static("static") - 这表示当客户端请求 [your_host]/static 然后使用静态处理程序来处理请求。每个配置的处理程序将按顺序运行,直到第一个匹配,因此如果没有文件匹配 css 目录下的请求,则下一个将运行。

files("css") - 这告诉静态处理程序在本地查看名为 css 的文件夹以提供静态内容(即客户端要求 [your_host]/static/style.css 将得到 [app_directory]/css/style.css

file("image.png") - 这告诉静态处理程序返回本地文件以提供静态内容(即客户端要求 [your_host]/static/* 将得到 [app_directory]/image.png

default("index.html") - 这将为任何请求提供本地文件 [app_directory]/index.html [your_host]/static/*

resource、resources 和 defaultResource 做同样的事情,但用于应用程序内置的资源,而不是文件系统上的资源。


推荐阅读