This module exposes ways of dealing with requests. Requests are only "acquired" when a new request incomes to the application. You should be able to access most of the original attributes of a request coming from Express with the methods exposed in this method.
Everything should be "read-only" so there is no way to change a request, only read from its attributes.
Defines the possible HTTP methods from a request. The available methods are described in this documentation for Express.
All requests have an attached unique identifier. This function exposes such id of the request being passed. Id's are just UUID (v4) strings and they are used to track requests inside and outside the Elm application.
myId =
Express.Request.id requestReturns the request's method. Nice to use when pattern matching for specific routes. For a good example on how this
could be used, look at the /example folder in the
repository/source.
method =
Express.Request.method requestThe url function will return an Elm Url describing the current request details. For more information, please
refer to Elm's original docs for URLs.
Because elm-express does not have a dedicated router, you can use the URL data to perform routing like you would if you were creating an SPA with Elm. Elm's guide on Parsing URLs should be a very good starting point to understand how we can leverage Elm's APIs with elm-express for routing.
For a more detailed example, look at the /example
folder in the repository/source.
url =
Express.Request.url requestGives access to the body of the request.
elm-express will always return the body as a String so it is your job to decode whatever is the content into the
desired type. If you are getting a JSON payload, you can leverage Elm's own
Json.Decode.decodeString and if it
is a form payload, Elm's Url.Parser.Query
should be up for the job.
simpleBody =
Express.Request.body requestThis exposes the Time.Posix of the moment the request arrived, relative to the server time.
rightNow =
Express.Request.now requestReturns all the request's headers in a Dict.
headers =
Express.Request.headers requestFetches a specific header from the request.
isAjax =
request
|> Express.Request.header "X-Requested-With"
|> Maybe.map ((==) "XMLHttpRequest")
|> Maybe.withDefault FalseReturns all the request's cookies in a Dict. Although Express makes a distinction between signed and unsigned
cookies, making them accessible with different attributes, all cookies, signed or not, will be be available in this
Dict.
cookies =
Express.Request.cookies requestGets the value of a specific cookie. All cookies, signed or not, can be accessed through this function.
lastVisit =
Express.Request.cookie "lastVisit" requestAllows access to specific session keys. Details on how you can setup your session are in the documentation for the
JavaScript portion of this library. Please refer to the README for that.
user =
Express.Request.session "user" requestThis is a helper function that takes a Json.Decode.Value (most likely a JSON object from JavaScript) and extracts
the requestId key from such Value. It is very useful when defining the decodeRequestId function when creating your
application with Express.application. For an example on how this can be used, check the
/example folder in the repository/source.
The
Requestis an opaque type that holds the data from the request. It should have most of the readable attributes as described in the Express API reference.