Middleware is a convenient way to validate requests, modify responses, and otherwise inject yourself into the HTTP request/response lifecycle.
Grind supports Connect middleware via Express, for full information on Connect middleware, visit the Connect site.
Global middleware is registered via app.routes.use()
:
approutes
This middleware will now apply to all routes registered after it’s been added. Any routes registered before will not have the middleware.
Unlike Express, you can’t pass a path to
use
. If you’d like to register middleware on a path, you’ll need to add it to a group with a prefix.
Group middlware provides an easy way to apply middleware to a collection of routes. Middleware defined on a group will apply only to routes registered in that group, even if two groups share the same prefix.
You can pass middleware to a group as part of it’s options:
approutes
You can also add middleware to the group via use
:
approutes
Middleware in groups cascade downward, so if you nest groups each group will inherit middleware from it’s ancestors and apply them all contained routes:
approutes
In this example, the hello
route will run with someMiddleware
, and the goodbye
route will run with someMiddleware
and otherMiddleware
. Each middleware will be called in the order it’s registered.
To add middleware to a specific route, you have a couple of options.
You can pass middleware to a route as part of it’s action:
approutes
You can also add middleware to the route after it’s been created:
approutes
Passing functions around for middleware can become a bit unwieldy, so Grind let‘s you register middleware onto the router with aliases.
To register middleware, simply call registerMiddleware
on the Router:
{approutes}MiddlewareProviderpriority = 100
Once you‘ve registered your alias, you can use it anywhere you’d normally add middleware:
Editapproutesapproutesapproutesapproutesapproutes