gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 19:40:38 25 4
gpt4 key购买 nike

我正在学习网络版 Clojure。我正在尝试在 Lein 应用程序中实现 Bidi 和 Ring。当我打开该网站时,出现 500 错误:

HTTP ERROR: 500

Problem accessing /. Reason:

Response map is nil

Powered by 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到浏览器。

关于web - Bidi、Ring 和 Lein 的简单 Web 应用程序给出 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60234899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com