RouteKit ·
RouteKit is a set of tools for generating lightweight and efficient routers.
packages | NPM version |
---|---|
routekit | |
routekit-resolver |
Installation
$ npm install -D routekit
Usage Example
Create a routes file: routes.build.js
#!/usr/bin/env node ; process.stdout.writeemitter r"/", Location.Home, r"/user/:id", Location.UserView, r"/user/:id/edit", Location.UserEdit,;
Run routes.build.js
file
$ node routes.build.js > routes.js
And it will generate routes.js
file with a compact flattened trie.
const ROUTES = f: 35 38 33 7 p: "user/" "/edit" s: 0 1 2;
Emmiter
Emitter generates flattened tries that can be used by routekit-resolver
for matching urls.
;;
inject
function injects code blocks into existing code using incode package.
This function is using routekit
prefix to detect injectable regions.
Resolver
Installation
NPM package routekit-resolver
provides a commonjs, es2015 modules and TypeScript typings.
npm install routekit-resolver
Usage Example
; // routekit:emit("routes")const ROUTES = f: 35 38 33 7 p: "user/" "/edit" s: 0 1 2;// routekit:end const match = ; ;// {// state: 1,// vars: ["123"],// }
API
;
resolve()
function has 2 parameters:
map
is a flattened trie generated by routekit.path
is a path that should be resolved.
When resolve function returns null
value it means that no match was found.