Chapter 7. Web Applications
7.0. Introduction
Web application development is the breadwinner for many languages nowadays; Clojure is no exception. In the annual 2013 State of Clojure Survey, web development ranked first for the question, “In which domains are you applying Clojure and/or ClojureScript?”
Much of the Clojure web development community today centers around Ring, an HTTP server library very much akin to Ruby’s Rack. Starting with the introduction to Ring in Recipe 7.1, “Introduction to Ring”, you’ll find a full complement of recipes here that will get you up to speed in no time.
Following Ring, the chapter takes a tour of the other Clojure web development ecosystems available at the time of writing, covering a few templating and HTML-manipulation libraries and a number of alternative web frameworks.
7.1. Introduction to Ring
Problem
You need to write an HTTP service with Clojure.
Solution
Clojure has no built-in HTTP server, but the de facto standard for serving basic, synchronous HTTP requests is the Ring library.
To follow along with this recipe, clone the https://github.com/clojure-cookbook/ringtest repository and overwrite src/ringtest.clj:
(nsringtest(:require[ring.adapter.jetty:asjetty]clojure.pprint));; Echo (with pretty-print) the request received(defnhandler[request]{:status200:headers{"content-type""text/clojure"}:body(with-out-str(clojure.pprint/pprintrequest))})(defn-main[];; Run the server on port 3000(jetty/run-jettyhandler{
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access