本文介绍了默认情况下,在Compojure中使用index.html为/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为 index.html
的静态文件,当有人请求 /
时,我想提供。通常,网络服务器,但Compojure不会。当有人请求 /
?
I have a static file called index.html
that I'd like to serve when someone requests /
. Usually web servers do this by default, but Compojure doesn't. How can I make Compojure serve index.html
when someone requests /
?
这是我用于静态目录的代码:
Here's the code I'm using for the static directory:
; match anything in the static dir at resources/public
(route/resources "/")
推荐答案
这将是一个非常简单的环中间件:
This would be a pretty simple Ring middleware:
(defn wrap-dir-index [handler]
(fn [req]
(handler
(update-in req [:uri]
#(if (= "/" %) "/index.html" %)))))
只要用这个函数包装你的路由, c $ c> / 在您的其余代码看到它们之前转换为 /index.html
的请求。
Just wrap your routes with this function, and requests for /
get transformed into requests for /index.html
before the rest of your code sees them.
(def app (-> (routes (your-dynamic-routes)
(resources "/"))
(...other wrappers...)
(wrap-dir-index)))
这篇关于默认情况下,在Compojure中使用index.html为/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!