Request
or Response
.
Create a middleware by conforming to the Middleware
protocol. It has a single function intercept
which takes a Request
and next
closure. It returns an EventLoopFuture<Response>
.
Request
Request
before it is handled, you can do so before calling next
. Be sure to call and return next
when you’re finished!
next(req)
when you are finished.
Middleware
to add some data to a Request
. For example, you may want to authenticate an incoming request with a Middleware
and then add a User
to it for handlers down the chain to access.
You can set generic data on a Request
using Request.set
and then access it in subsequent Middleware
or handlers via Request.get
.
For example, you might be doing some experiments with a homegrown ExperimentConfig
type. You’d like to assign random configurations of that type on a per-request basis. You might do so with a Middleware
:
Middleware
and utilize the set ExperimentConfig
in your handlers.
Response
Response
of the handled request, you can plug into the future returned by next
.
Middleware
intercept requests.
Application
, you can add it via Application.useAll
.
Middleware
can be setup to only intercept requests to specific handlers via the .use(_ middleware: Middleware)
function on an Application
. The Middleware
will intercept all requests to the subsequently defined handlers.
.group
function that takes a Middleware
. The Middleware
will only intercept requests handled by handlers defined in the closure.