From 0167d5b4bdfae0186e1ec16ee85e5f49ced4383c Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Sat, 8 Oct 2022 17:36:22 +0300 Subject: Update README --- README.md | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5f3b8d5..ad68b5f 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,11 @@ Each route consists of three items: A string path starting with a forward slash describing the URL path to match. -To create parameters from the path, prepend a colon (:) in front of a path slice like you would with a Clojure keyword. For example a string such as `/hi/:name` would match any string that matches the `\/hi\/.*` regex. The `:name` itself will then be available with its value from the `request` passed to the response function, like this: +To create parameters from the path, prepend a colon (:) in front of a path slice like you would with a Clojure keyword. + +##### Required parameters + +A required parameter with a string such as `/hi/:name`, which would match any string that matches the `\/hi\/.*` regex in the URI, in its own slice. The `:name` itself will then be available with its value from the `request` passed to the response function, like this: ```clojure (fn [req] @@ -120,11 +124,20 @@ To create parameters from the path, prepend a colon (:) in front of a path slice :body (str "Hi, " name)})) ``` -Additionally, you may want to use an optional parameter, in which case you'd want to add a question mark to the end of it, like `/hi/:name?`, which will match the `\/hi\/?.*?` regex, meaning that the previous forward slash is optional, and what comes after that is also optional. +##### Optional parameters + +A optional parameter with a string such as `/hi/:name?`, which would match any string that matches the `\/hi\/?.*?` regex in the URI, in its own slice. If there is a `:name` provided in the URI then it will then be available with its value from the `request` passed to the response function, like this: + +```clojure +(fn [req] + (let [name (:name (:params req))] + {:status 200 + :body (str "Hi, " name)})) +``` -##### Wildcard matching +##### Wildcard parameters -The above-mentioned `:name` and `:name?` only match in its own sequence, e.g inside a space of two slashes. They cannot, by design, match the whole URL path. If you need wildcard matching, instead use `:name*`, which will match everything, including forward slashes. +The above-mentioned `:name` and `:name?` only match in its own path slice, e.g inside a space surrounded by two forward slashes. They cannot, by design, match the whole URL path. If you need wildcard matching, instead use `:name*`, which will match everything, including forward slashes. #### `:method` -- cgit v1.2.3