nova_basic_handler

Types

erlydtl_vars/0
mod_fun/0

Functions

handle_json(X1, ModFun, Req, State) ->

Handler for JSON. It takes two different return objects:

Options

{json, JSON :: map()} returns the JSON encoded to the user. If the operation was a POST the HTTP-status code will be 201, otherwise 200.

{json, StatusCode :: integer(), Headers :: map(), JSON :: map()} Same operation as the above except you can set custom status code and custom headers.

handle_ok(X1, ModFun, Req, State) ->

Handler for regular views. This will render a template with given variables. If not another view is specified in options a view that corresponds to the controller will be rendered.


  -module(my_first_controller).
  -compile(export_all).
 
  my_function(_Req) ->
     {ok, []}.
  
The example above will then render the view named 'app_main.dtl'

Options can be specified as follows:

- view - Specifies if another view should be rendered instead of default one

- headers - Custom headers
handle_status(X1, ModFun, Req, State) ->

Handler for returning http status codes. There's three different ways one can return status code. The most basic case is {status, Status} where Status is the code that should be returned.

If there's a need for additional headers to be sent along with the http code one can specify a third argument that is a map with header-fields.

One can also send in a body as a fourth argument in the tuple. It can either be a binary or a map. If it's a map it will be concidered a JSON-structure and encoded.
handle_redirect(X1, ModFun, Req, State) ->
Handles redirects. This will return a 302-status code with a location given by the user. Something like {redirect, "/login"} will send a 302 with location set to "/login"
handle_cowboy_req(X1, ModFun, Req, State)