Middleware
Middleware
Sometimes, it becomes to recycle code to run on multiple routes. Angel allows for this in the form of middleware. Middleware are frequently used as authorization filters, or to serialize database data for use in subsequent routes. Middleware in Angel can be any route handler, whether a function or arbitrary data. You can also throw exceptions in middleware.
Denying Requests via Middleware
A middleware should return either true
or false
. If false
is returned, no further routes will be executed. If true
is returned, route evaluation will continue. (more on request handler return values here).
As you can imagine, this is perfect for authorization filters.
Declaring Middleware
You can call a router's chain
method (recommended!), or assign middleware in the middleware
parameter of a route method.
Named Middleware
Router
instances allow you to assign names to middleware via registerMiddleware
. After registering a middleware, you can include it in a route just by passing its name into the middleware
array. If you are mount
-ing another Routable
or Angel
instance, you can map all its middleware into a namespace by passing it to the use
call.
Global Middleware
To add a handler that handles every request, call app.use
. This is equivalent to calling app.all('*', <handler>)
. (more info on request lifecycle here).
For more complicated middleware, you can also create a class:
Canonically, when using a class as a request handler, it should provide a handleRequest(RequestContext, ResponseContext)
method. This pattern is seen throughout many Angel plugins, such as VirtualDirectory
or Proxy
.
The reason for this is that a name like handleRequest
makes it very clear to anyone reading the code what it is supposed to do. This is the same rationale behind controllers providing a configureServer
method.
waterfall
You can chain middleware (or any request handler together), if you do not feel like making multiple chain
calls, or if it is impossible to call chain multiple times, using the waterfall
function:
Maintaining Code Readability
Note that a cleaner representation is:
In general, consider it a code smell to stack multiple handlers onto a route like this; it hampers readability, and in general just doesn't look good.
Instead, when you have multiple handlers, you can split them into multiple waterfall
calls, assigned to variables, which have the added benefit of communicating what each set of middleware does:
Tip: Prefer using named functions as handlers, rather than anonymous functions, or concrete objects.
Next Up...
Take a good look at controllers in Angel!
Last updated