从compojure提供index.html文件 [英] Serving index.html file from compojure
问题描述
这里是我的handler.clj
我是新的clojure和compojure并尝试与compojure一起创建一个基本的web应用程序。 > (ns gitrepos.handler
(:require [compojure.core:refer:all]
[compojure.route :as route]
[ring.util.response:as resp]
[ring.middleware.defaults:refer [wrap-defaults site-defaults]]))
defroutes app-routes
(GET /[](resp / file-responseindex.html{:rootpublic}))
(route / not-foundNot Found )
(def app
(wrap-defaults app-routes site-defaults))
我在 / resources / public 下有 index.html 文件,但应用程序无法呈现此html文件。 未找到
我搜索了很多,即使是在Compojure中默认情况下在/默认情况下提供index.html似乎不能解决此问题。
不知道我在这里丢失了什么。
也许你想尝试使用一些模板库,作为 Selmer 。所以你可以这样做:
(defroutes myapp
(GET/ hello /[]
(render-string(read-templatetemplates / hello.html))))
或传递一些值:
(defroutes myapp
(GET/ hello /[name]
(render-string(read-templatetemplates / hello.html){name:Jhon}))
并且,作为@ piotrek-Bzdyl说:
(GET/ index.html{:rootpublic}))
I'm new to clojure and compojure and trying out the compojure along with ring to create a basic web application.
here is my handler.clj
(ns gitrepos.handler
(:require [compojure.core :refer :all]
[compojure.route :as route]
[ring.util.response :as resp]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]))
(defroutes app-routes
(GET "/" [] (resp/file-response "index.html" {:root "public"}))
(route/not-found "Not Found"))
(def app
(wrap-defaults app-routes site-defaults))
i have this index.html file under /resources/public but the application is not rendering this html file. Instead getting Not found
I have searched a lot it, even this Serve index.html at / by default in Compojure does not seems to resolve the issue.
Not sure what am i missing here.
Maybe you want to try use some template library, such as Selmer. So you can do something like this:
(defroutes myapp
(GET "/hello/" []
(render-string (read-template "templates/hello.html"))))
Or passing some value:
(defroutes myapp
(GET "/hello/" [name]
(render-string (read-template "templates/hello.html") {name: "Jhon"})))
And, as @piotrek-Bzdyl said:
(GET "/" [] (resource-response "index.html" {:root "public"}))
这篇关于从compojure提供index.html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!