Define the Routes with Reitit

Web Development with Clojure, Third Edition — by Dmitri Sotnikov, Scot Brown (17 / 107)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Extend Ring | TOC | HTML Templating Using Selmer 👉

Reitit is a general purpose routing library that can be used to provide routing on top of Ring as well as for client-side routing, as we’ll see later on. It provides a way to associate handler functions with a URL and an HTTP method.

We already saw some examples of routing using Reitit when we built our first application in Chapter 1, Getting Your Feet Wet. Now let’s take a closer look at the functionality it provides. First, add it as a dependency in the ring-app project.

ring-app-5/project.clj

​ :dependencies [[org.clojure/clojure ​"1.10.1"​]
​ [metosin/muuntaja ​"0.6.7"​]
​ [metosin/reitit ​"0.5.11"​]
​ [metosin/ring-http-response ​"0.9.1"​]
​ [ring ​"1.8.2"​]]

With the dependency in place, let’s update the namespace to reference reitit.ring and add a route for the / URI.

ring-app-5/src/ring_app/core.clj

​ (​ns​ ring-app.core
​ (:require
​ [reitit.ring :as reitit]
​ [muuntaja.middleware :as muuntaja]
​ [ring.adapter.jetty :as jetty]
​ [ring.util.http-response :as response]
​ [ring.middleware.reload :refer [wrap-reload]]))

​ ​;;...​

​ (​def​ routes
​ [[​"/"​ {:get html-handler}]])


​ (​def​…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.