@zenweb/router
v6.6.0
Published
Zenweb Router module
Readme
ZenWeb Router module
ZenWeb 所使用的路由模块
Install
npm i @zenweb/routerimport { Core } from '@zenweb/core';
import router from '@zenweb/router';
const app = new Core();
app.setup(router());
app.start();
// GET: /
app.router.get('/', ctx => {
ctx.body = 'static-index'; // static-index
});
// GET: /param/testval
app.router.get('/param/:name', ctx => {
ctx.body = 'param value: ' + ctx.params.name; // param value: testval
});
// GET: /param/prefix99999suffix
app.router.get('/param/prefix:{name}suffix', ctx => {
ctx.body = 'param value: ' + ctx.params.name; // param value: 99999
});
// GET: /param/with-regex/99999
app.router.get('/param/with-regex/:id(\\d+)', ctx => {
ctx.body = 'param value: ' + ctx.params.id; // param value: 99999
});
// GET: /wildcardany/some/path
app.router.get('/wildcard*', ctx => {
ctx.body = 'wildcard: ' + ctx.params.wildcard; // wildcard: any/some/path
});
// GET: /some/pathsuffix
app.router.get('*suffix', ctx => {
ctx.body = 'wildcard: ' + ctx.params.wildcard; // wildcard: /some/path
});
// GET: /betweenany/valuesuffix
app.router.get('/between*suffix', ctx => {
ctx.body = 'wildcard: ' + ctx.params.wildcard; // wildcard: any/value
});
// GET: /regex/aaa
app.router.get(/^\/regex\/(aaa|bbb)$/i, ctx => {
ctx.body = 'regex: ' + ctx.params.$1; // regex: aaa
});Benchmark
{ totalRootRouters: 6000, totalSubRouters: 0 } x 10000 times
Static | example: /test733
| implement | total | ms/req | faster | | -- | -- | -- | -- | | koa-router | 5997 | 0.5997 | - | | zen-router | 6 | 0.0006 | 1000x |
Param | example: /param160/5dcc08b5-f702-4ae0-9dc3-264a5dcf187a
| implement | total | ms/req | faster | | -- | -- | -- | -- | | koa-router | 6442 | 0.6442 | - | | zen-router | 25 | 0.0025 | 258x |
Wildcard | example: /45d83a7a-491c-4481-80a5-77e112b563ab/491suffix
| implement | total | ms/req | faster | | -- | -- | -- | -- | | koa-router | 6400 | 0.64 | - | | zen-router | 247 | 0.0247 | 26x |
Regex | example: /regex746/31f0843d-329f-4d62-b354-166ffc78e6f0
| implement | total | ms/req | faster | | -- | -- | -- | -- | | koa-router | 6252 | 0.6252 | - | | zen-router | 896 | 0.0896 | 7x |
Random | static, param, regex, wildcard
| implement | total | ms/req | faster | | -- | -- | -- | -- | | koa-router | 6376 | 0.6376 | - | | zen-router | 339 | 0.0339 | 19x |
Not Found
| implement | total | ms/req | faster | | -- | -- | -- | -- | | koa-router | 4969 | 0.4969 | - | | zen-router | 708 | 0.0708 | 7x |
{ totalRootRouters: 6000, totalSubRouters: 12000 } x 10000 times
Static | example: /test287
| implement | total | ms/req | faster | | -- | -- | -- | -- | | koa-router | 53092 | 5.3092 | - | | zen-router | 682 | 0.0682 | 78x |
Param | example: /sub1/param149/e653d812-c1e0-4fc0-9769-386815c42d54
| implement | total | ms/req | faster | | -- | -- | -- | -- | | koa-router | 54796 | 5.4796 | - | | zen-router | 694 | 0.0694 | 79x |
Wildcard | example: /0dc74cd0-2f43-4cbe-80b0-ae0b81db78d1/395suffix
| implement | total | ms/req | faster | | -- | -- | -- | -- | | koa-router | 46600 | 4.66 | - | | zen-router | 618 | 0.0618 | 75x |
Regex | example: /regex721/77dfaf19-caad-4fb7-9ebe-8a9153265dd8
| implement | total | ms/req | faster | | -- | -- | -- | -- | | koa-router | 55195 | 5.5195 | - | | zen-router | 1175 | 0.1175 | 47x |
Random | static, param, regex, wildcard
| implement | total | ms/req | faster | | -- | -- | -- | -- | | koa-router | 47189 | 4.7189 | - | | zen-router | 868 | 0.0868 | 54x |
Not Found
| implement | total | ms/req | faster | | -- | -- | -- | -- | | koa-router | 46008 | 4.6008 | - | | zen-router | 1141 | 0.1141 | 40x |
