{"id":14989475,"url":"https://github.com/acodercat/koa-router-pro","last_synced_at":"2025-07-28T07:09:14.323Z","repository":{"id":143870990,"uuid":"72832005","full_name":"acodercat/koa-router-pro","owner":"acodercat","description":"基于koa-router中间件基础之上的一个扩展版本，把路由文件跟控制器独立开来，写法类似于Sails的routes.js文件。并且实现了根据目录路径自动注册路由。","archived":false,"fork":false,"pushed_at":"2016-11-04T09:29:15.000Z","size":4,"stargazers_count":6,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-09T13:21:13.967Z","etag":null,"topics":["koa","koa-router","koajs","router"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/acodercat.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-11-04T09:13:49.000Z","updated_at":"2023-04-11T07:34:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"bc5955d4-fe2e-4a9d-a83e-8d0f06c330cf","html_url":"https://github.com/acodercat/koa-router-pro","commit_stats":{"total_commits":3,"total_committers":1,"mean_commits":3.0,"dds":0.0,"last_synced_commit":"fd44e5ff9ac6a5c86458de87e081c661eaa7d9b3"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/acodercat/koa-router-pro","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acodercat%2Fkoa-router-pro","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acodercat%2Fkoa-router-pro/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acodercat%2Fkoa-router-pro/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acodercat%2Fkoa-router-pro/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/acodercat","download_url":"https://codeload.github.com/acodercat/koa-router-pro/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acodercat%2Fkoa-router-pro/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267476258,"owners_count":24093464,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-28T02:00:09.689Z","response_time":68,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["koa","koa-router","koajs","router"],"created_at":"2024-09-24T14:18:25.631Z","updated_at":"2025-07-28T07:09:14.301Z","avatar_url":"https://github.com/acodercat.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# koa-router-pro\n\n\n\n##  koa-router-pro v1\n\n  ` koa-router-pro@1.0.0` 是基于koa-router中间件基础之上的一个扩展版本，把路由文件跟控制器独立开来，写法类似于Sails的routes.js文件。并且实现了根据目录路径自动注册路由。\n\n```js\nvar routerPro = require('koa-router-pro');\nvar router = routerPro({//这里会返回koa-router的实例\n  root:'C:\\\\Users\\\\Cat\\\\Desktop\\\\nodejs\\\\app\\\\controllers',//controller的根目录\n  routes:[{routes:{'get /' : 'IndexController.test','post /' : '/user/InfoController.test'},prefix:'/v1'}],//一个数组每个数组元素是一个包含routes和prefix的对象prefix为可选项\n  prefix:'/v1',//路由前缀，可选项\n  autoLoad:false//自动路由的配置，会根据配置的root目录来自动注册目录及其子目录下的所有控制器文件，默认是true。如果设置为false就会关闭。\n\n});\napp.use(router.routes());\n```\n\n  通常把routes配置选项通过rquire路由文件来获得，比如var routes = rquire('routes.js');。\n  可以把routes.js路由文件拆分成多个路由文件比如userRoutes.js，adminRouter.js然后配置到routes数组选项中去\n\n```js\nmodule.exports.prefix = '/user';//路由文件中的路由前缀，可选项\nmodule.exports.routes = {\n  '/info':function *(next){//支持直接写处理函数\n    yield *next;\n  },\n  'post /':'/user/InfoController.test',\n  'get /blog/(\\\\d{4}-\\\\d{2}-\\\\d{2})' : '/user/InfoController.test',//支持正则\n  '/':'IndexController.test',//如果不写请求方式，即为all\n  '/test':{\n    name:'test',//给路由取名，通过router.url(name)，来得到路由url\n    methods:['get','post'],//支持多种请求模式，不写则为all\n    handler:'/user/InfoController.test',//表示user目录下的InfoController控制器下的test方法\n  }\n}\n```\n\n## 安装\n\n```\n$ npm install koa-router-pro --save\n```\n\n\n## 例子\n\n\n\n\nroutes.js 路由支持正则如：get /blog/(\\\\d{4}-\\\\d{2}-\\\\d{2})\n\n```js\nmodule.exports.prefix = '/user';//路由文件中的路由前缀，可选项\nmodule.exports.routes = {\n  '/info':function *(next){//支持直接写处理函数\n    yield *next;\n  },\n  'post /':'/user/InfoController.test',\n  '/':'IndexController.test',//如果不写请求方式，即为all\n  '/test':{\n    name:'test',//给路由取名，通过router.url('test')，来得到路由url\n    methods:['get','post'],//支持多种请求模式，不写则为all\n    handler:'/user/InfoController.test',//表示user目录下的InfoController控制器下的test方法\n  }\n}\n```\nroot目录下的IndexController.js 默认会自动把root目录下的以Controller结尾的js文件注册到路由中去，url为路径加控制器命加方法名,routes里面的路由如果跟自动路由重复，routes配置项中的路由如果跟自动路由重复，routes配置项中的路由会覆盖自动路由\n```js\n\nmodule.exports = {\n  test:function *(next){//路由就是/index/test\n    this.body = 'test';\n    yield next;\n  },\n};\n\n```\napp.js\n```js\nvar routes = rquire('routes.js');\nvar routerPro = require('koa-router-pro');\nvar router = routerPro({\n  root:'C:\\\\Users\\\\Cat\\\\Desktop\\\\nodejs\\\\app\\\\controllers',//controller的根目录\n  routes:[routes],//一个数组每个数组元素是一个包含routes和prefix的对象prefix为可选项\n  prefix:'/v1'//路由前缀，可选项\n});\napp.use(router.routes());\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Facodercat%2Fkoa-router-pro","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Facodercat%2Fkoa-router-pro","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Facodercat%2Fkoa-router-pro/lists"}