Installations
npm install egg-router-group
Developer Guide
Typescript
Yes
Module System
N/A
Min. Node Version
>=8.0.0
Node Version
8.10.0
NPM Version
5.6.0
Score
70
Supply Chain
99.3
Quality
75.4
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
zzzs
Download Statistics
Total Downloads
58,585
Last Day
12
Last Week
100
Last Month
549
Last Year
6,784
GitHub Statistics
33 Stars
21 Commits
2 Forks
2 Watching
1 Branches
1 Contributors
Package Meta Information
Latest Version
1.1.0
Package Id
egg-router-group@1.1.0
Unpacked Size
16.63 kB
Size
5.62 kB
File Count
8
NPM Version
5.6.0
Node Version
8.10.0
Total Downloads
Cumulative downloads
Total Downloads
58,585
Last day
-58.6%
12
Compared to previous day
Last week
-26.5%
100
Compared to previous week
Last month
11.6%
549
Compared to previous month
Last year
-28.1%
6,784
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
egg-router-group
have the ability to route group operations for eggjs.
赋予eggjs路由群组操作的能力。
Install
1$ npm i egg-router-group --save
Mount
1// {app_root}/config/plugin.js 2exports.routerGroup = { 3 enable: true, 4 package: 'egg-router-group', 5};
Features
为 app.router 注入了路由群组 group(options, cb) 方法, options: 路由群组公用属性, cb: 路由回调。
路由群组允许你共用路由属性,例如:路由别名前缀,路由url前缀,中间件等,你可以利用路由群组统一为多个路由设置共同属性,而不需在每个路由上都设置一次。共用属性被指定为对象格式,当作 app.router.group 方法的第一个参数,具体路由群组的相关内容,可参考下面几个常用样例来熟悉这些特性。
目前支持这三种属性
- 路由别名前缀: name
- 路由url前缀: prefix
- 中间件: middlewares
Usage
基本用法
1// {app_root}/app/router.js 2module.exports = app => { 3 const { router, controller, middleware } = app; 4 const m1 = middleware.m1(); 5 const m2 = middleware.m2({ key: 'value' }); 6 const m3 = middleware.m3(); 7 8 router.group({ name: 'home::', prefix: '/pre', middlewares: [ m1, m2 ] }, router => { 9 // router-path: /pre/test, middlewares: m1, m2 10 // router-path: /pre/test2, middlewares: m1, m2 11 router.get('/test', controller.home.test).get('/test2', controller.home.test); 12 // router-name: home::post, router-path: /pre/post, middlewares: m1, m2, m3 13 router.post('post', '/post', m3, controller.home.post); 14 15 // others 16 router.all('/test', controller.home.all1); 17 router.all('testname', '/test2', controller.home.all2); 18 router.put('/put', controller.home.put); 19 router.delete('/delete', controller.home.delete); 20 router.del('/del', controller.home.del); 21 router.options('/options', controller.home.options); 22 router.patch('/patch', controller.home.patch); 23 router.redirect('/redirect', '/'); 24 router.redirect('/redirect2', 'home::testname', 302); 25 }); 26 27 // 设置单个属性 28 router.group({ name: 'home::' }, router => { 29 // router-path: /test 30 router.get('/test', controller.home.test); 31 // router-name: home::post, router-path: /post 32 router.post('post', '/post', controller.home.post); 33 }); 34 35 // group 同样支持链式操作 36 router.group({ name: 'home::', prefix: '/pre', middlewares: [ m1, m2 ] }, router => { 37 // router-path: /pre/test, middlewares: m1, m2 38 // router-path: /pre/test2, middlewares: m1, m2 ⚠️这里🈶️group 设置的属性哦 39 router.get('/test', controller.home.test).get('/test2', controller.home.test); 40 }) 41 .get('/test3', controller.home.test); // router-path: /test3 ⚠️这里🈚group 设置的属性哦 42};
中间件
支持传单个中间件或数组的形式
1// {app_root}/app/router.js 2module.exports = app => { 3 const { router, controller, middleware } = app; 4 const m1 = middleware.m1(); 5 const m2 = middleware.m2({ key: 'value' }); 6 7 router.group({ middlewares: [ m1, m2 ] }, router => { 8 router.get('/test_m1', controller.home.test_m1); 9 router.post('/test_m2/:id', controller.home.test_m2); 10 }); 11 router.group({ middlewares: m1 }, router => { 12 router.get('/test_m3', controller.home.test_m3); 13 router.post('/test_m4/:id', controller.home.test_m4); 14 }); 15};
路由别名前缀 & 路由url前缀
路由别名前缀 & 路由url前缀 只支持string类型
1// {app_root}/app/router.js 2module.exports = app => { 3 // name 4 router.group({ name: 'home::' }, router => { 5 // router-name: home::post 6 router.post('post', '/test/:id', controller.home.t1); 7 }); 8 9 // prefix 10 router.group({ prefix: '/pre' }, router => { 11 // router-name: test, router-path: /pre/test2 12 router.get('test', '/test2', controller.home.t2); 13 }); 14};
Advanced Usage
路由子群组,顾名思义,即路由群组内也可以使用路由群组功能,很适用于大型项目,当路由到达一定量级时,也能轻松维护管理。如果你想的话,路由群组可以一直嵌套下去。
注:如果出现设置重复的中间件,该中间件也会被执行多次,这部分自由度还是会开放给用户,不做特殊处理。
1 2 // {app_root}/app/router.js 3 router.group({ name: 'home1::', prefix: '/pre1', middlewares: m1 }, router => { 4 // router-name: home1::name_g1, router-path: /pre1/test_g1, middlewares: m1, m2 5 router.get('name_g1', '/test_g1', m2, controller.group.g1); 6 7 router.group({ prefix: '/pre2', middlewares: m2 }, router => { 8 // router-path: /pre1/pre2/test_g3, middlewares: m1, m2, m2 9 router.get('/test_g3', m2, controller.group.g1); 10 11 // router-path: /pre1/pre2/test_g4/:id, middlewares: m1, m2 12 // router-path: /pre1/pre2/test_g4_1/:id, middlewares: m1, m2 13 router.post('/test_g4/:id', controller.group.g2).get('/test_g4_1', controller.group.g2); 14 15 router.group({ name: 'home2::' }, router => { 16 // router-name: home1::home2::name_g6, router-path: /pre1/pre2/test_g6/:id, middlewares: m1, m2 17 router.post('name_g6', '/test_g6/:id', controller.group.g2); 18 // ... 19 }); 20 }) 21 .get('/test_g2_1', controller.group.g1); // router-path: /pre1/test_g2_1, middlewares: m1 22 });
Questions & Suggestions
Please open an issue here.
License
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 existing vulnerabilities detected
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/21 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
3
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More