在 Compojure 中默认在/处提供 index.html [英] Serve index.html at / by default in Compojure

查看:24
本文介绍了在 Compojure 中默认在/处提供 index.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 index.html 的静态文件,我想在有人请求 / 时提供它.通常 Web 服务器默认执行此操作,但 Compojure 不会.当有人请求 / 时,如何让 Compojure 提供 index.html 服务?

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

推荐答案

这将是一个非常简单的 Ring 中间件:

This would be a pretty simple Ring middleware:

(defn wrap-dir-index [handler]
  (fn [req]
    (handler
     (update-in req [:uri]
                #(if (= "/" %) "/index.html" %)))))

只需用这个函数包装你的路由,/ 的请求就会在你的其余代码看到它们之前转换为 /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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆