【问题标题】:Ring/Compojure serving index.html in subfolders?Ring/Compojure 在子文件夹中提供 index.html?
【发布时间】:2014-10-10 18:36:22
【问题描述】:

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

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

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

【问题讨论】:

    标签: clojure compojure ring


    【解决方案1】:

    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))))))
    

    【讨论】:

      【解决方案2】:

      您可以通过提供一个解构向量来轻松做到这一点,如下所示:

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

      更多详情请看这里: https://github.com/weavejester/compojure/wiki/Destructuring-Syntax

      【讨论】:

        猜你喜欢
        • 2016-06-20
        • 2013-05-25
        • 2014-05-11
        • 2011-12-05
        • 2011-12-10
        • 2020-04-25
        • 2020-08-08
        • 2015-04-16
        • 2017-09-09
        相关资源
        最近更新 更多