Add an entry to the middlewares
. Usually you want to keep the standard Cowboy
handlers afterwards, but you can replace them if you want.
{ok, Pid} = cowboy:start_clear(ListenerName,
[{port, Port}],
#{
env => #{dispatch => Dispatch},
middlewares => [my_middleware, cowboy_router, cowboy_handler]
}
),
A middleware implements the behavior cowboy_middleware
.
- You cannot change a response after it has been sent. For example, if you add a
middleware after the generic
cowboy_handler
middleware. If a previous middleware has called thecowboy_req:reply/4
function the response has already been sent on the wire. For that purpose you need to use a stream handler instead.
Thanks for showing, what the official documentation doesn't :)