Trail is a router for koa with the option to explicitly execute multiple routes on a single request (similar to express routing).
For example, assume we have an authenticate middleware function which should run on every
/api/* route. Instead of:
// chain the authenticate on every routeapp;app;app;app;
We can simply apply the authentication handler once on a wildcard route:
appall'/api/*' authenticate;// the above route will be called before of any of the below routes matching /api/*// therefore, we don't need to explicitly chain it on each route.app;app;app;app;
This reduces chaining on individual routes, and may improve security since there is less likelihood of a developer forgetting to add important security middleware on certain routes.
npm install koa-trail
var koa = ;var trail = ;var app = ;app;//now we can attach routesapp;app;
app is passed as a parameter to trail, it creates a method on the
app for every http method (get, post, put, delete, etc), plus app.all, app.register, and app.url. If you would prefer not to "pollute" the
app object with these methods, you may initialize a router object without passing the app parameter.
var koa = ;var trail = ;var app = ;var router = ;// pass the router.handler object, not the router object itselfapp;// now attach routes to the router object instead of approuter;router;
Routes can be named which makes them