【发布时间】:2016-03-13 09:15:38
【问题描述】:
我想为任何 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 <
Unexpected token 是index.html 中的第一个<
所以我的问题是如何调整我的 Compojure/Ring/Immutant 堆栈以与 react-router 配合使用?
更新: 我还尝试了以下中间件,将所有请求更改为根但没有运气:
(defn wrap-dir-index [handler]
(fn [req]
(handler
(assoc req :uri "/"))))
【问题讨论】:
-
看看stackoverflow.com/questions/35828884/… - 当您使用该代码时,来自
resources/public的所有文件都将由服务器提供,所有以/结尾的路径都将提供/index.html。 -
我希望每个 GET 都服务于 /index.html,而不仅仅是以 / 结尾的那个。我已经用其他替代方法更新了我的问题,但我仍然无法让事情按我的意愿工作。
-
其他资源如 javascript 和 css 呢?它们都嵌入到您的 index.html 中了吗?或者,如果找不到特定资源,您可能想提供
resources/public中的任何内容并回退到提供index.html的内容? -
是的,这正是我想要做的:) 但是,当尝试使用建议帖子中的代码时,它不起作用。