我正在网上学习Clojure。我正在尝试在Lein应用程序中实现Bidi和Ring。当我打开网站时,出现500错误:
HTTP错误:500
问题访问/。原因:
响应图为零
由Jetty提供支持://
我的文件如下:
./ project.clj
(defproject bidi-ring "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[org.clojure/clojure "1.10.0"]
[bidi "2.1.6"]
[ring/ring-core "1.6.3"]
[ring/ring-jetty-adapter "1.6.3"]]
:repl-options {:init-ns my.app}
:main my.app)
./ src / my / handler.clj
(ns my.handler (:gen-class)
(:require [bidi.ring :refer (make-handler)]
[ring.util.response :as res]))
(defn index-handler
[request]
(res/response "Homepage"))
(defn article-handler
[{:keys [route-params]}]
(res/response (str "You are viewing article: " (:id route-params))))
(def handler
(make-handler ["/" {"index.html" index-handler
["articles/" :id "/article.html"] article-handler}]))
./ src / my / app.clj
(ns my.app (:gen-class)
(:require [my.handler :refer [handler]]
[ring.middleware.session :refer [wrap-session]]
[ring.middleware.flash :refer [wrap-flash]]))
(use 'ring.adapter.jetty)
(defn -main []
(def app
(-> handler
wrap-session
wrap-flash))
(run-jetty app {:port 3000}))
如何解决?
您在路线上犯了一个错误。看看这个Bidi演示仓库:
https://github.com/io-tupelo-demo/bidi
tst.demo.core
第16行的代码显示了正在发生的情况。在bidi
中,路径“ /index.html”不包含退化的路径“ /”:
(let [routes ["/" ; common prefix
{"index.html" :index
"article.html" :article}]
routes-2 ["/" ; common prefix
{"" :index ; add a default route so "/" and "/index.html" both => `:index` handler
"index.html" :index
"article.html" :article}]]
(is= (bidi/match-route routes "/index.html") {:handler :index}) ; normal case
(is= (bidi/match-route routes "/") nil) ; plain "/" route is not available, has no default
(is= (bidi/match-route routes-2 "/") {:handler :index})) ; Now we get the :index route
[当没有路由匹配时,match-route
返回一个nil
,然后服务器将其转换为发送给浏览器的500 not-found
。