我有一个名为 index.html
的静态文件,我希望在有人请求 /
时提供该文件。通常是网络服务器do this by default ,但 Compojure 没有。当有人请求 /
时,如何让 Compojure 提供 index.html
服务?
这是我用于静态目录的代码:
; match anything in the static dir at resources/public
(route/resources "/")
最佳答案
另一种方法是在附加路由中创建重定向或直接响应。就像这样:
(ns compj-test.core
(:use [compojure.core])
(:require [compojure.route :as route]
[ring.util.response :as resp]))
(defroutes main-routes
(GET "/" [] (resp/file-response "index.html" {:root "public"}))
(GET "/a" [] (resp/resource-response "index.html" {:root "public"}))
(route/resources "/")
(route/not-found "Page not found"))
“/”路由返回公共(public)文件夹中存在的“index.html”文件响应。 “/a”路由通过“内联”文件index.html 直接响应。
有关响铃响应的更多信息:https://github.com/mmcgrana/ring/wiki/Creating-responses
编辑:删除了不必要的[ring.adapter.jetty]
导入。
关于clojure - 在 Compojure 中默认提供 index.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7729628/