clojure - 在 Compojure 中默认提供 index.html

标签 clojure compojure

我有一个名为 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/

相关文章:

java - 将像素映射到数据

clojure - 让 Leiningen 和 Cygwin 工作

clojure - 追加到嵌套关联结构中

clojure - Compojure 路由丢失参数信息

clojure - 在 Clojure 中重构时的结构正确性

java - 如何在 Clojure 中创建可分发的应用程序?

clojure - compojure/clojure GET 路由与 CSS 给出纯文本样式表响应

postgresql - Korma 和 Postgresql 的默认 ID?

ajax - cljs-ajax POST 给出 403

java - 将 Clojure WAR Web 应用程序部署到 Tomcat 后提取的结果应该是什么?