APIHttp.Server.Route

Http.Server.Route

Routing requests to handlers by method and path.

A route’s pattern is a path whose segments are literal or :name, which captures one decoded segment. dispatch tries routes in order.

Param

type Param =    { name : String    , value : String    }Source

A path segment captured by a :name pattern segment.

Fields

name

name : StringSource

The name after the :.

value

value : StringSource

The decoded segment.

Instances:

Eq Param
Show Param

Route

type Route e =    { method : String    , pattern : String    , handler : List Param -> Request e ->{e} Response e    }Source

A handler for requests with one method whose path matches a pattern.

Fields

method

method : StringSource

The method it answers, such as "GET".

pattern

pattern : StringSource

The path pattern, such as "/users/:id".

handler

handler : List Param -> Request e ->{e} Response eSource

Answers a matching request, given its captured parameters in order.

dispatch

dispatch : List (Route e) -> (Request e ->{e} Response e) -> Request e ->{Protocol, e} Response eSource

Answers the request with the first route whose method and pattern match. When the path matches only routes of other methods, it answers 405 with an Allow header; when nothing matches, fallback answers. Path segments are percent-decoded first; a malformed escape, invalid UTF-8, or an encoded / or \ fails with Malformed.

lookup

lookup : String -> List Param -> Maybe StringSource

The value captured under name.

import Http.Server.Route exposing (Param(..))

Http.Server.Route.lookup "id" [{ name = "id", value = "7" }] == Just "7"
Http.Server.Route.lookup "user" [] == Nothing

path

path : String -> StringSource

The path of a request target, without its query, and taken out of an absolute-form target.

import Http.Server.Route

Http.Server.Route.path "/a/b?x=1" == "/a/b"
Http.Server.Route.path "http://host/a" == "/a"