phylum
Koa extension with focus on efficient routing and modern technologies.
Requirements
Please note that phylum
requires nodejs >= 10 to support latest features such as named regexp groups for parsing route paths.
Installation
npm i phylum
Changelog
The changelog documents all major changes made between releases.
Quick Start
const Phylum = ; // app works the same as a koa application expect of additional routing:const app = ; // Add handlers with routes:app;
API
class Phylum
A Phylum
instance is a Koa
application that works as documented here.
In addition to the koa api, a phylum instance exposes all routing functions as documented below.
const Phylum = ; const app = ;
class Router
A Router
instance is just routes requests and is not a Koa application itself.
const Koa = ;const Router = ; const router = ; const app = ;app;
app/router.use([path, ][options, ]handler)
Register routed middleware or handler functions.
app;
- path
<route>
- The route path as explained below. - options
<object>
- Route options as explained below.
app/router.METHOD([path, ][options, ]handler)
Register routed handler functions.
The handler is called only if the request method matches
the specified one and there is no unrouted rest path:
app;
METHOD
- One of the following:get
post
put
head
delete
options
- path
<route>
- The route path as explained below. - options
<object>
- Route options as explained below (withoutmethod
option).
router.handler(options)
Get a function that handles a context using the router.
Useful for embedding the router into an existing koa application or another routing library.
app;
- options
<object>
- An optional object with the following options:- cleanRestPath
<any>
- If truthy, the context'srestPath
property will be deleted before passing the context to the router and will be added again before calling the next function. This is useful for nesting the router into another routing library. - cleanParams
<any>
- If truthy, the context'sparams
property will be deleted before passing the context to the router and will be added again before calling the next function.
- cleanRestPath
Options
rewrite
If truthy, path and url will be rewritten to match the unrouted rest path.
Trailing slashes will be preserved.
app;
method
If set, the handler is called only if the request method matches
the specified one and there is no unrouted rest path:
app;
Routes
Routes can be strings, regular expressions or a combination of both using array.
The following are valid routes:
'/foo/bar''foo' 'bar/''foo' /bar/'/foo' '/bar'
- Duplicate, leading and trailing slashes are ignored.
- Regular expressions are only matched against a single part of a path.
Route Parameters
Route parameters can be defined using named regular expression capturing groups:
app;
Rest Path
While routing, phylum keeps track of the rest path and stores it on the context so routers can be nested in each other. The rest path is an array containing unrouted path segments:
app;
Contributing
Suggestions to improve this package are always welcome.
Feel free to file issues and to submit pull requests.
Tests
npm install # Run linter & tests: npm test# Run tests and watch for changes: npm run watch