HttpServer

Create a server that can respond to HTTP requests.

You write your server using The Elm Architecture by subscribing to request events and responding with commands in update.

See examples/http-server for a working example.

Initialization

type Permission

The permission to start a Server.

You get this from initialize.

type Server

The HTTP server.

type ServerError
= ServerError String String

Error code and message from node. Most likely from a failed attempt to start the server (e.g. EADDRINUSE). Refer to the node docs for details.

initialize : Task Permission

Initialize the HttpServer module and get permission to create a server.

createServer :
Permission
-> { host : String, port_ : Int }
-> Task ServerError Server

Task to initialize a Server.

Requests

type alias Request =
{ headers : Dict String String
, method : Method
, body : Bytes
, url : Url
}

An incoming HTTP reqest.

type Method
= GET
| HEAD
| POST
| PUT
| DELETE
| CONNECT
| TRACE
| PATCH
| UNKNOWN String

HTTP request methods.

bodyAsString : Request -> Maybe String

Get request body as a string.

bodyFromJson : Decoder a -> Request -> Result Error a

Get request body as json.

requestInfo : Request -> String

Get a string representation of the request.

Good for logging.

Responding to requests

onRequest : Server -> (Request -> Response -> msg) -> Sub msg

Subscribe to incoming HTTP requests.

See HttpServer.Response for more details on responding to requests.