Clojure ring response下载index.html而不是呈现它
我有一个Clojure ring response下载index.html而不是呈现它,clojure,compojure,ring,Clojure,Compojure,Ring,我有一个index.html位于resources/public/index.html中,并定义了以下路径(应用程序被拆分得更多,只是为了使代码简洁): 但是,当我转到localhost:8090/时,它会下载html文件而不是呈现它 如果我转到localhost:8090/index.html它会正确地呈现文件,因此我假设我的路由不正确,但在查看示例后,我不太确定原因。这与此完全相同 您需要创建一个中间件来更新您的请求: (defn wrap-dir-index [handler] (fn
index.html
位于resources/public/index.html
中,并定义了以下路径(应用程序被拆分得更多,只是为了使代码简洁):
但是,当我转到localhost:8090/
时,它会下载html文件而不是呈现它
如果我转到
localhost:8090/index.html
它会正确地呈现文件,因此我假设我的路由不正确,但在查看示例后,我不太确定原因。这与此完全相同
您需要创建一个中间件来更新您的请求:
(defn wrap-dir-index [handler]
(fn [req]
(handler
(update-in req [:uri]
#(if (= "/" %) "/index.html" %)))))
然后包装您的路线:
(def app
(wrap-dir-index (wrap-defaults app-routes site-defaults)))
完成。使用以下方法:
(:require [clojure.java.io :as io]
[ring.middleware.resource :as resource])
(defroutes routes
(GET "/" []
(io/resource "index.html")))
还可以使用中间件进行资源包装
(resource/wrap-resource "/public")
此问题可能重复的问题已解决。@kongeor使用
resp/redirect
仍会导致html文件被下载而不是呈现
(resource/wrap-resource "/public")