在Compojure中对react-router browserHistory进行匹配和GET请求

时间:2016-03-13 09:15:38

标签: clojure compojure

我想为任何GET请求路径提供以下路由,以便我可以使用前端路由库(react-router)。

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

这就是我的尝试:

(GET "/*" [] (resource-response "index.html" {:root "public"}))
(rfn [] (resource-response "index.html" {:root "public"}))
(GET "/:any{.*}" [] (resource-response "index.html" {:root "public"}))
(GET "/*" [] (ring.util.response/content-type
                     (ring.util.response/resource-response "index.html" {:root "public"}) "text/html"))
(GET "/*" [] (clojure.java.io/resource "public/index.html"))

他们都给出了同样的错误:

Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:5000/index.css".
bundle.js:1 Uncaught SyntaxError: Unexpected token <

意外令牌是<

中的第一个index.html

所以我的问题是如何使我的Compojure / Ring / Immutant堆栈适应反应路由器?

更新: 我还尝试了以下midleware,将所有请求更改为root但没有运气:

(defn wrap-dir-index [handler]
  (fn [req]
    (handler
     (assoc req :uri "/"))))

1 个答案:

答案 0 :(得分:1)

您可以使用Ring wiki page上所述的wrap-resource以及捕捉所有路线:

(defroutes handler
  (GET "/" []
    (-> (resource-response "index.html" {:root "public"})
        (content-type "text/html))
  (GET "/*" [] (redirect "/")))

(def app
  (-> handler
      (wrap-resource "public")
      (wrap-content-type)
      (wrap-not-modified))

我认为最好从所有其他网址重定向到“/”,以便您的应用程序始终使用相同的基址。