Creating a function plug

Function plugs are similar to module plugs, but they are contained in a function instead of a module. This means that we don't have to call the init to arrange the options. However, function plugs receive two arguments, exactly like the call function in module plugs: the connection and the options. The following function is a function plug:

def my_function_plug(conn, _options) do  conn  |> put_resp_content_type("text/plain")  |> send_resp(200, "Hello #{conn.assigns.username}")end

In this function, we take the connection and pass it through to put_resp_content_type, which will modify the connection to set the content type of the response. Then, we take this modified connection to send a response, setting 200 as the status ...

Get Mastering Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.