Compojure Routing


#1

Eric, I was going back through your web development with Clojure video and could not seem to find an answer to a question I have. While you cover how to structure a route such as /users/:id…what if you’re looking for the web-site end-user to provide a high-end and a low end of a range so you can then provide detail within that range like…/users/age?min_age=min&max_age=max…how would you structure that in compojure? (By the way, when I use the structure as shown I get a NumberFormatException on “age”.

Thanks


#2

Hi @randre03,

Thanks for the question!

I think Compojure has changed a little bit since I made that course a few years ago.

Your question is about getting the query params and destructuring. Here are the Compojure docs for reference: https://github.com/weavejester/compojure/wiki/Destructuring-Syntax#compojure-specific-destructuring

(GET "/users/age" [min_age max_age] (search-users-by-age min_age max_age))

But I believe you will also need to wrap all of your routes in a compojure.handler/site like in the example here: https://github.com/weavejester/compojure-example/blob/master/src/compojure/example/routes.clj#L15

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

I hope that helps! Let me know.

Rock on!
Eric