Serve index.html at / by default in Compojure

前端 未结 8 1891
无人及你
无人及你 2020-12-28 14:19

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

8条回答
  •  被撕碎了的回忆
    2020-12-28 14:35

    This would be a pretty simple Ring middleware:

    (defn wrap-dir-index [handler]
      (fn [req]
        (handler
         (update-in req [:uri]
                    #(if (= "/" %) "/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)))
    

提交回复
热议问题