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.
Do you have a source for this?
It appears to me that stream handlers and middleware accomplish different goals. Middleware are a feature that the default
cowboy_stream_h
stream handler supports, right? Where you could hook in between, say, the router and the underlying application handler. If you wanted to do that with a stream handler, you'd need to reimplement the routing logic...