【问题标题】:Compojure/Clojure error with routes路由的 Compojure/Clojure 错误
【发布时间】:2014-03-25 20:32:19
【问题描述】:

我正在使用 Clojure/ring/compojure 制作一个网络应用程序,但遇到路由问题。我有两个文件:web.clj 和landing.clj。我想将导航到 uri 从 web.clj 处理程序的用户路由到landing.clj 处理程序,并调用 home 函数,该函数将呈现我的应用程序的首页。我一生都无法理解语义,请帮忙。我阅读的文档假设有很多网络开发知识,而且我是初学者。当我从 Leiningen 运行本地服务器时,我现在在浏览器中收到 404 错误消息。我知道它正在通过landing.clj文件中的defroutes,因为地址栏显示http://0.0.0.0:5000/landing,当我加载http://0.0.0.0:5000时。

这是我的 web.clj 文件的代码,它成功地重定向到了landing.clj 文件:

  (defroutes app
  (ANY "/repl" {:as req}
       (drawbridge req))
  (GET "/" [] (redirect "landing")) ;; come fix this later
  (ANY "*" []
       (route/not-found (slurp (io/resource "404.html")))))

这是来自landing.clj 文件,错误位于GET "/" 函数的某处:

(defroutes app
  (GET "/" []
       {:status 200
        :headers {"Content-Type" "text/html"}
        :body (home)})
  (POST "/" [weights grades] ((process-grades (read-string weights) (read-string grades)) ))
  (ANY "*" []
       (route/not-found (slurp (io/resource "404.html")))))

【问题讨论】:

    标签: redirect clojure routes compojure ring


    【解决方案1】:

    下面的表达式似乎有额外的括号:

    (POST "/" [weights grades] ((process-grades (read-string weights) (read-string grades)) ))

    应该是:

    (POST "/" [weights grades] (process-grades (read-string weights) (read-string grades)) )

    【讨论】:

    • 感谢您指出这一点。我想知道是否有办法让我直接从 web.clj defroutes 调用landing.clj 中的 home 函数而不使用重定向。你能告诉我在这种情况下什么是惯用的吗?
    • 可以将landing.clj命名空间导入web.clj并调用home函数。
    • 哈哈!这就是我需要知道的。很简单。 (掌心)
    猜你喜欢
    • 1970-01-01
    • 2014-05-30
    • 2011-04-23
    • 2013-04-20
    • 1970-01-01
    • 2021-07-19
    • 2012-10-11
    • 1970-01-01
    • 2014-09-13
    相关资源
    最近更新 更多