ktor - 带有 ktor 的静态目录 - 不在“mysite.com/static/...”下
问题描述
我通读了Serving Static Content并且脑子有问题。
我有一个文件夹/resources/static
。它里面有静态的东西。其中一些是 index.html、favicon.ico、css 文件夹、js 文件夹等。
我希望它显示为https://example.org/favicon.ico、https://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")
}
}
解决方案
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 做同样的事情,但用于应用程序内置的资源,而不是文件系统上的资源。
推荐阅读
- php - 可以从类外部覆盖 __get
- android - 如何使用 android 应用从服务器获取更新?
- reactjs - useState 不强制重新渲染反应组件
- node.js - 如何从html中获取具有值的脚本标签
- python - 快速 Pandas.DataFrame 多列实例化
- eclipse - 在 Eclipse 2021-06 中导入 maven 项目时卡在安装 JAX-RS facet
- prometheus - 带 SUM 的乘法度量
- javascript - 有没有办法使用谷歌浏览器在一个标签中禁用硬件加速?
- sql - 通过三表SQL比较两表
- java - 如何在springboot中使用@manytomany关系在数据库中添加记录