Search code examples
f#saturn-framework

F# Saturn Framework: what is the difference between 'get' and 'forward'?


for example, the following code inside a router computation exppression

get "/time" (warbler (fun _ -> System.DateTime.Now.ToString("HH:mm:ss") |> sprintf "Time now is %s" |> text))

seems to have exactly the same effect as

forward "/time" (warbler (fun _ -> System.DateTime.Now.ToString("HH:mm:ss") |> sprintf "Time now is %s" |> text))

In both cases, the current time is displayed whenever a client navigates to /time.

In what situations will these be different?


Solution

  • you can supply any HttpHandler to forward, so it doesn't surprise me that you see this behavior. Where forward becomes really useful is for nesting entire sub-routers under a certain path root. In this case, forward makes sure that the routes under the child path match only on the sub-portions of the route.

    For example, take this setup:

    let routerA = router {
     get "/foo" (text "hi")
    }
    
    let routerB = router {
     forward "/hi" routerA
    }
    

    In this case, the full path to the routerA get method would be /hi/foo, not /foo.