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.
Trying to find now where I read something about this but cannot remember. I thought it was somewhere in the official docs, repo or some comment from Loïc, but alas…
There is the slight possibility that I mixed it up with hooks which were explicitly deprecated in favor of stream handlers.