gpt4 book ai didi

clojure - Ring/Compojure 在子文件夹中提供 index.html?

转载 作者:行者123 更新时间:2023-12-04 00:35:15 27 4
gpt4 key购买 nike

我的 ressources/public 目录包含许多子文件夹,每个子文件夹都包含一个 index.html。我如何设置路由,以便它们以递归方式监视任何 GET“.../”路径并返回 index.html 而不在 URL 中显示文件?

一种方法是像下面这样显式设置每条路线,但我希望不需要定义每条路径。

(GET  "/" [] (resource-response "index.html" {:root "public"}))
(GET "/foo" [] (resource-response "foo/index.html" {:root "public"}))
...

最佳答案

ring wrap-resources 的小修改中间件可以解决问题:

(defn wrap-serve-index-file
[handler root-path]
(fn [request]
(if-not (= :get (:request-method request))
(handler request)
(let [path (.substring (codec/url-decode (:uri request)) 1)
final-path (if (= \/ (or (last path) \/))
(str path "index.html")
path)]
(or (response/resource-response path {:root root-path})
(handler request))))))

关于clojure - Ring/Compojure 在子文件夹中提供 index.html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25353289/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com