【问题标题】:error while composing routes in compojure在 compojure 中编写路由时出错
【发布时间】:2013-02-24 02:20:47
【问题描述】:

以下组合路由有效。

(defroutes app-routes
  (GET "/" [] (index))
  (GET "/twauth" [] (tw/authorize))
  (ANY "/twcallback" [] (do
                          (tw/callback)
                          (index)))
  (route/resources "/")
  (route/not-found "Not Found"))

(def app (handler/site app-routes))

但是我收到以下错误。它抛出一个 java.nullpointer.exception。我在这里做错了什么?

(defroutes 应用程序路由 (GET "/" [] (索引)) (GET "/twauth" [] (tw/授权)) (ANY "/twcallback" [] (做 (tw/回调) (索引))))

(defroutes base-routes
  (route/resources "/")
  (route/not-found "Not Found"))

(def app
  (-> app-routes
      base-routes
      handler/site))

【问题讨论】:

    标签: clojure compojure


    【解决方案1】:

    您的基本路由匹配所有请求。我认为以下内容更好地说明了这一点:

    (defroutes base-routes
      (route/not-found "Not found"))
    
    (def app
      (-> app-routes
          base-routes
          handler/site))
    

    无论你在上面的 app-routes 中做什么,base-routes 都会在之后检查并且总是返回 not-found。请注意,每个请求都通过两个路由进行线程处理,而不是第一个匹配获胜。

    因此,您需要将基本路由作为后备路由移动到您的应用程序路由中——就像您在工作示例中所做的那样——或者在应用程序中编写它们:

    (def app
      (-> (routes app-routes base-routes)
          handler/site))
    

    这里的组合路由确保第一场比赛获胜。

    【讨论】:

      猜你喜欢
      • 2014-03-25
      • 2011-04-23
      • 2013-04-20
      • 2013-12-21
      • 2012-05-31
      • 2018-10-18
      • 2012-12-31
      • 2021-07-19
      • 2012-10-11
      相关资源
      最近更新 更多