Bidi、Ring和Lein的简单web应用程序给出了500个错误

Bidi、Ring和Lein的简单web应用程序给出了500个错误,web,server,clojure,ring,bidi,Web,Server,Clojure,Ring,Bidi,我正在为网络学习Clojure。我正在尝试在Lein应用程序中实现Bidi和Ring。当我打开站点时,我得到一个500错误: HTTP错误:500 访问/时出现问题。原因: 响应图为零 由码头提供动力:// 我的文件如下: /project.clj ./src/my/handler.clj ./src/my/app.clj 如何修复它?您在路线上犯了一个错误。看看这个Bidi回购演示: tst.demo.core第16行的代码显示了正在发生的事情。在bidi中,route/index.html不

我正在为网络学习Clojure。我正在尝试在Lein应用程序中实现Bidi和Ring。当我打开站点时,我得到一个500错误:

HTTP错误:500

访问/时出现问题。原因:

响应图为零

由码头提供动力://

我的文件如下:

/project.clj

./src/my/handler.clj

./src/my/app.clj


如何修复它?

您在路线上犯了一个错误。看看这个Bidi回购演示:

tst.demo.core第16行的代码显示了正在发生的事情。在bidi中,route/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。

感谢您提供如此详细的答案!在Clojureland帮助其他了不起的人让我非常高兴。
(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}]))
(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}))
  (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