响铃下载index.html而不是渲染它

时间:2016-05-27 08:06:36

标签: clojure compojure ring

我在index.html中有一个resources/public/index.html,并定义了以下路由(应用程序的分割比这更多,只是简化了代码):

(ns example.example
  (:require [compojure.route :as route]))

(defroutes routes
           (GET "/" [] (resource-response "index.html" {:root "public"} "text/html")))

(defroutes application-routes
           routes
           (route/resources "/")
           (route/not-found (resource-response "index.html" {:root "public"} "text/html")))


(def application
  (wrap-defaults application-routes site-defaults))

然而,当我转到localhost:8090/时,它会下载html文件而不是渲染它。

如果我转到localhost:8090/index.html它会正确呈现文件,所以我认为我的路由不正确,但在查看示例后我不太清楚为什么。

2 个答案:

答案 0 :(得分:1)

这与此question完全相同。

您需要创建一个中间件来更新您的请求:

JObject json = JObject.Parse("{\"@STARTDATE\": \"'2016-02-17 00:00:00.000'\",\"@ENDDATE\": \"'2016-02-18 23:59:00.000'\"}");
var key = "@STARTDATE";

var value = GetJArrayValue(json, key);

private string GetJArrayValue(JObject yourJArray, JToken key)
{
    string value = "";
    foreach (JToken item in yourJArray.Children())
    {
        var itemProperties = item.Children<JProperty>();
        //If the property name is equal to key, we get the value
        var myElement = itemProperties.FirstOrDefault(x => x.Name == key.ToString());
        value = myElement.Value.ToString(); //It run into an exception here because myElement is null
        break;
    }
    return value;
}

然后包裹您的路线:

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

完成handler.clj

答案 1 :(得分:1)

使用此:

(:require [clojure.java.io :as io]
          [ring.middleware.resource :as resource])

(defroutes routes

     (GET "/" []
             (io/resource "index.html")))

还使用中间件进行资源包装

(resource/wrap-resource "/public")