在Compojure中合并路由

17

我有两个不同的基于Compojure开发的Clojure Web应用程序。每个应用程序使用defroutes创建其处理程序。

我该如何将这两个不同的defroutes定义合并为一个defroute? 我想重用第一个应用程序的路由到第二个应用程序中。

1个回答

27

您可以使用compojure.core/routes来组合路由:

 (def my-handler
    (routes some-handler
            some-other-handler))

我尝试了以下代码:(def route1 [(GET "/" [] (index-page)) (route/resources "/") (route/not-found "Page not found")]) (def allroutes (concat routes1 routes2)) (def app (handler/site (apply routes allroutes))) 但是第二个路由没有被使用。 - z1naOK9nu8iY5A
4
好的,我找到了错误:需要注意在第一个列表中没有找到路由处理程序... - z1naOK9nu8iY5A
1
好的,我找到了错误:需要注意在第一个列表中没有找到路由处理程序的情况... - 请详细说明一下?你发现了什么解决方案? - PPPaul
5
或许有点晚了,但我认为重点在于not-found路由应该是路由列表中的最后一个,否则它将匹配所有的URL,导致应用程序始终返回“未找到”。 - ak.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接