Functions only return the last expression evaluated, so if you have a function like:
(defn foo []
1
2
3)
Then the function will return 3. The same principle applies with routes, which are just anonymous functions: only the last one will be returned from the function.
In order to return more than one route, you need to combine them into a bigger route. Compojure has a function to do this, called "routes". This function is also the one used by the "defroutes" macro, which is literally the combination of "def" and "routes", in the same way that "defn" is the combination of "def" and "fn".
So you could write:
(defn route-from-to [uri f]
(routes
(GET (str uri ":id") [id]
(default-json-page f id))
(GET (str uri ":id/:from/:to") [id from to]
(default-json-page f id from to))))
Or if we want to make use of the context macro:
(defn route-from-to [uri f]
(context uri
(GET "/:id" [id]
(default-json-page f id))
(GET "/:id/:from/:to" [id from to]
(default-json-page f id from to))))
- James