Koa branch router
Koa branch router is a simple radix tree (-ish) router for koa.
- There seem to be no radix tree routers with nested router support (at the time of writing).
- Popular routers handle middlewares in an inconvenient way. See Middleware Routing.
- Express-style routing using verbs like
- Nested routes and middlewares.
- Path params and wildcard capturing.
const Koa = ;const Router = ;const app = ;const router = ;router;app;app;
Middlewares are only called if a handler registered in the same router matches agaist the path.
Middlewares and Handlers are essentially the same except:
// treated as a middleware if added using .use()router// treated as a handler if added using .all() or .get(), .post() etcrouterall'/' handler
// the path `/users/42/images`router
If you dont want this behaviour, you should use a route fragment.
// the path `/users/42/images`router// <-- called// fragments don't add a routing boundary and// behave like a middleware registered on the// parent router.
You may nest routers.
const userRouter =// becomes `/users/`; // becomes `/users/:id`const tokenRouter = prefix: '/tokens'// becomes `/auth/tokens/`; // becomes `/auth/tokens/:id`const fileRouter =// remains `/files/`); // remains `/files/:id`const rootRouter = router;
Named parameters like
:name match a single path segment delimited by
Catches paths starting with the provided path, and captures the rest until the end.
Wildcards can be used without capturing as well.
- Captured parameters are decoded using
decodeURIComponent, whereas wildcard captures are not.
Initialize a new router.
||Case sensitive paths|
Registers handlers for path. Supported verbs are:
router// alias for `.delete()`allpath ...; // matches all methods
You may pass multiple handlers.
Registers middleware for path
// or multiplerouter;
// or with pathrouter;
Returns router middleware.
Initialize a new route fragment. Fragments don't add a routing boundary and behave like a middleware registered on the parent router. See Middleware Routing.
Accepts the same options as a router. See Router Options
The API is again similar to the router except that there is no
.routes() method. Only routers can be mounted on apps.
This object contains key-value pairs of named route parameters.
// GET /user/42router;