Choosing routes

Let's move on to configuring a few routes in our application. Before we do that, we'll need to open up two more modules at the beginning of App.fs:


open Suave open Suave.Filters open Suave.Operators open Suave.Successful open Suave.Web

Now, let's create a couple of routing rules, so that our application responds with different content for different paths. To achieve that, we can use the choose function, which takes a list of WebParts, and chooses the first one that applies (returns Some), or if none WebPart applies, then choose will also return None:


let webPart = choose [ path "/" >=> (OK "Home") path "/store" >=> (OK "Store") path "/store/browse" >=> (OK "Browse") path "/store/details" >=> (OK "Details") ]

Hover over path symbol in above snippet to inspect this function's type. What it means is that if we give it a string, it will return a WebPart. Under the hood, the function looks at the incoming request and returns Some if the path matches, and None otherwise. The >=> operator comes also from Suave library. It composes two WebParts into one by first evaluating the WebPart on the left, and applying the WebPart on the right only if the first one returned Some.

After applying the change try out all paths from the snippet by appending them to localhost URL your application is running under.

namespace Suave
module Filters

from Suave
module Operators

from Suave
module Successful

from Suave
module Web

from Suave
val webPart : WebPart<HttpContext>

Full name: SuaveMusicStore.webPart
val choose : options:WebPart<'a> list -> WebPart<'a>

Full name: Suave.WebPart.choose
val path : pathAfterDomain:string -> WebPart

Full name: Suave.Filters.path
val OK : body:string -> WebPart

Full name: Suave.Successful.OK
val startWebServer : config:SuaveConfig -> webpart:WebPart -> unit

Full name: Suave.Web.startWebServer
val defaultConfig : SuaveConfig

Full name: Suave.Web.defaultConfig

Show code from this section on GitHub

results matching ""

    No results matching ""