Clojure 使用compojure.route/resources和ring.middleware.resource/wrap-resource
我正试图通过和学习clojure web开发,我对Clojure 使用compojure.route/resources和ring.middleware.resource/wrap-resource,clojure,compojure,ring,Clojure,Compojure,Ring,我正试图通过和学习clojure web开发,我对compojure.route/resources和ring.middleware.resource/wrap resource的用法有点不清楚 我已经查看了API文档,以及compojure.route和ring.middleware.resource的源代码。但是,我仍然不清楚是否需要同时使用compojure.route/resources路由和ring.middleware.resource/wrap resource中间件,或者comp
compojure.route/resources
和ring.middleware.resource/wrap resource
的用法有点不清楚
我已经查看了API文档,以及compojure.route
和ring.middleware.resource
的源代码。但是,我仍然不清楚是否需要同时使用compojure.route/resources
路由和ring.middleware.resource/wrap resource
中间件,或者compojure.route/resources
是否负责所有事情
还是一个优化的问题?与中一样,使用wrap resource
可以避免compojure路由开销?如果您有任何见解,我们将不胜感激。主要区别在于仅从特定路径提供资源:
(let [root (:root options "public")]
(resource-response (str root "/" resource-path))
但是ring.middleware.resource/wrap resource
在未找到任何资源的情况下在处理程序上提供了故障恢复机制:
(or (response/resource-response path {:root root-path})
(handler request))
正如您从函数中看到的,两个备选方案都使用:
(defn resource-response
"Returns a Ring response to serve a packaged resource, or nil if the
resource does not exist.
Options:
:root - take the resource relative to this root"
如果未找到请求的资源,则返回nil
因此,包裹资源
替代方案更适合在您已经有路线的情况下进行链接,如:
(defroutes routes
(GET "/" [] "<h1>Hello World</h1>")
(route/not-found "<h1>Page not found</h1>"))
(def app (-> routes
(wrap-resource "public/resources"))
(defroutes resources-routes
(route/resources "/"))
(defroutes routes
(GET "/" [] "<h1>Hello World</h1>")
(route/not-found "<h1>Page not found</h1>"))
(def app
(compojure.core/routes
resources-routes
routes))