Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure ring response下载index.html而不是呈现它_Clojure_Compojure_Ring - Fatal编程技术网

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