Liking cljdoc? Tell your friends :D

Route Syntax

Routes are defined as vectors of String path and optional (non-sequential) route argument child routes.

Routes can be wrapped in vectors and lists and nil routes are ignored.

Paths can have path-parameters (:id) or catch-all-parameters (*path). Since version 0.3.0, parameters can also be wrapped in brackets, enabling use of qualified keywords {user/id}, {*user/path}. The non-bracket syntax might be deprecated later.


Simple route:


Two routes:


Routes with route arguments:

[["/ping" ::ping]
 ["/pong" {:name ::pong}]]

Routes with path parameters:


Route with catch-all parameter:


Nested routes:

 ["/admin" {:middleware [::admin]}
  ["" ::admin]
  ["/db" ::db]]
 ["/ping" ::ping]]

Same routes flattened:

[["/api/admin" {:middleware [::admin], :name ::admin}]
 ["/api/admin/db" {:middleware [::admin], :name ::db}]
 ["/api/ping" {:name ::ping}]]


Reitit does not apply any encoding to your paths. If you need that, you must encode them yourself. E.g., /foo bar should be /foo%20bar.


Normal path-parameters (:id) can start anywhere in the path string, but have to end either to slash / (currently hardcoded) or to en end of path string:


Bracket path-parameters can start and stop anywhere in the path-string, the following character is used as a terminator.


Having multiple terminators after a bracket path-path parameter with identical path prefix will cause a compile-time error at router creation:

[["/files/file-{name}.pdf"]            ;; terminator \.
 ["/files/file-{name}-{version}.pdf"]] ;; terminator \-

Slash Free Routing


Generating routes

Routes are just data, so it's easy to create them programmatically:

(defn cqrs-routes [actions]
  ["/api" {:interceptors [::api ::db]}
   (for [[type interceptor] actions
         :let [path (str "/" (name interceptor))
               method (case type
                        :query :get
                        :command :post)]]
     [path {method {:interceptors [interceptor]}}])])
  [[:query   'get-user]
   [:command 'add-user]
   [:command 'add-order]])
; ["/api" {:interceptors [::api ::db]}
;  (["/get-user" {:get {:interceptors [get-user]}}]
;   ["/add-user" {:post {:interceptors [add-user]}}]
;   ["/add-order" {:post {:interceptors [add-order]}}])]

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close