mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-07 07:16:42 +02:00
Updated compojure per weavejester's suggestions
This commit is contained in:
@@ -81,18 +81,26 @@ The body may be a function, which must accept the request as a parameter:
|
|||||||
(GET "/" [] (fn [req] "Do something with req")))
|
(GET "/" [] (fn [req] "Do something with req")))
|
||||||
```
|
```
|
||||||
|
|
||||||
Route patterns may include named parameters,
|
Or, you can just use the request directly:
|
||||||
|
|
||||||
|
```clojure
|
||||||
|
(defroutes myapp
|
||||||
|
(GET "/" req "Do something with req"))
|
||||||
|
```
|
||||||
|
|
||||||
|
Route patterns may include named parameters:
|
||||||
|
|
||||||
```clojure
|
```clojure
|
||||||
(defroutes myapp
|
(defroutes myapp
|
||||||
(GET "/hello/:name" [name] (str "Hello " name)))
|
(GET "/hello/:name" [name] (str "Hello " name)))
|
||||||
```
|
```
|
||||||
|
|
||||||
You can match entire paths with *
|
You can adjust what each parameter matches by supplying a regex:
|
||||||
|
|
||||||
```clojure
|
```clojure
|
||||||
(defroutes myapp
|
(defroutes myapp
|
||||||
(GET "/file/*.*" [*] (str *)))
|
(GET ["/file/:name.:ext" :name #".*", :ext #".*"] [name ext]
|
||||||
|
(str "File: " name ext))
|
||||||
```
|
```
|
||||||
|
|
||||||
Handlers may utilize query parameters:
|
Handlers may utilize query parameters:
|
||||||
@@ -123,7 +131,7 @@ Or, for POST and PUT requests, form parameters
|
|||||||
The return value of a route block determines at least the response body
|
The return value of a route block determines at least the response body
|
||||||
passed on to the HTTP client, or at least the next middleware in the
|
passed on to the HTTP client, or at least the next middleware in the
|
||||||
ring stack. Most commonly, this is a string, as in the above examples.
|
ring stack. Most commonly, this is a string, as in the above examples.
|
||||||
But, you may also return a [response body](https://github.com/mmcgrana/ring/blob/master/SPEC):
|
But, you may also return a [response map](https://github.com/mmcgrana/ring/blob/master/SPEC):
|
||||||
|
|
||||||
```clojure
|
```clojure
|
||||||
(defroutes myapp
|
(defroutes myapp
|
||||||
@@ -222,4 +230,6 @@ You can easily read in templates from your resources directory. Here's a helper
|
|||||||
|
|
||||||
Further reading:
|
Further reading:
|
||||||
|
|
||||||
[Clojure for the Brave and True](http://www.braveclojure.com/)
|
* [Official Compojure Documentation](https://github.com/weavejester/compojure/wiki)
|
||||||
|
|
||||||
|
* [Clojure for the Brave and True](http://www.braveclojure.com/)
|
||||||
|
Reference in New Issue
Block a user