{"id":14989594,"url":"https://github.com/yss/koa-route-tree","last_synced_at":"2026-02-01T16:32:44.418Z","repository":{"id":143882861,"uuid":"67409586","full_name":"yss/koa-route-tree","owner":"yss","description":"Path is route, path is parameter, with quickly, fastly, efficiently and restfully.","archived":false,"fork":false,"pushed_at":"2020-02-28T09:28:34.000Z","size":19,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-28T12:15:08.057Z","etag":null,"topics":["koa","koa-middleware","koa2","middleware","node","restful","route","router"],"latest_commit_sha":null,"homepage":"","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/yss.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-09-05T09:57:35.000Z","updated_at":"2020-02-28T09:28:03.000Z","dependencies_parsed_at":null,"dependency_job_id":"211d8210-e2dc-474f-aeef-8c8077563741","html_url":"https://github.com/yss/koa-route-tree","commit_stats":{"total_commits":16,"total_committers":1,"mean_commits":16.0,"dds":0.0,"last_synced_commit":"12e6a5a760b6a22a5bc8581f37d71a8ffc34db05"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/yss/koa-route-tree","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yss%2Fkoa-route-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yss%2Fkoa-route-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yss%2Fkoa-route-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yss%2Fkoa-route-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yss","download_url":"https://codeload.github.com/yss/koa-route-tree/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yss%2Fkoa-route-tree/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28982733,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-01T16:29:42.054Z","status":"ssl_error","status_checked_at":"2026-02-01T16:29:41.428Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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-middleware","koa2","middleware","node","restful","route","router"],"created_at":"2024-09-24T14:18:37.479Z","updated_at":"2026-02-01T16:32:44.401Z","avatar_url":"https://github.com/yss.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## koa-route-tree\n    Path is route, also path is parameter.\n    The most fast and efficient route.\n    Now, it support restful route mode.\n\nLet's see it.\n\n## Install\n\n`npm install koa-route-tree --save`\n\n## Caution!\n\nThis is only support for koa@2+!\n\nIf you use koa@1, install it with version 1.4.0. `npm install koa-route-tree@1.4.0 --save`\n\n## Usage\n\n```js\n// File: app.js\n\nconst Koa = require('koa');\nconst Route = require('koa-route-tree');\n\nconst app = new Koa();\n\nconst fileRouter = ['robots.txt'];\napp.use(Route(__dirname + '/controller', {\n    '/appList': '/app/list',\n    'favicon.ico': '/index/favicon.ico'\n}, async function (ctx, next, controller) {\n    let pathname = ctx.path.substring(1);\n    \n    if (fileRouter.indexOf(pathname) \u003e -1) {\n        ctx.body = 'Allows: *';\n        return;\n    }\n    \n    // console.log(controller);\n    // and do next if you want\n    // await next();\n}));\n```\n## Restful mode example\n\n```js\n// File: controllers/app/list.js\n\n/**\n * Normal Get Request, Support urls:\n * 1. /app/list =\u003e page: undefined | second: undefind\n * 2. /app/list/0 =\u003e page: 0 | second: undefined\n * 4. /app/list/1/a =\u003e page: 1 | second: a\n */\nexports.index = function (page, second) {\n    this.body = 'Page/' + page + '/Second/' + second;\n};\n\n// restful route mode\n/**\n * For GET Request. Support urls:\n * 1. GET /app/list/set =\u003e page: undefined | second: undefind\n * 2. GET /app/list/0/set =\u003e page: 0 | second: undefined\n * 3. GET /app/list/1/set/a =\u003e page: 1 | second: a\n * 4. GET /app/list/set/1/a =\u003e page: 1 | second: a\n */\nexports.set = function (page, second) {\n    this.body = 'GET Page/' + page + '/Second/' + second;\n};\n\n/**\n * The same as the above GET request\n */\nexports.postSet = function (page, second) {\n    this.body = 'POST Page/' + page + '/Second/' + second;\n};\n/**\n * The same as the above GET request\n */\nexports.putSet = function (page, second) {\n    this.body = 'PUT Page/' + page + '/Second/' + second;\n};\n```\n\n## Class\n\n`Route(controllerDirectory[, alias][, withoutRouteHandler])`\n\n### Parameters\n\n* `controllerDirectory` is a string of controller directory.\n* `alias` is a object for alias to a path or function.\n\n    ```js\n    const alias = {\n        '/d': '/index/download', // a short download link\n        'a/b': 'your/path' // the same link, this is equivalent to /a/b \u003c=\u003e /your/path\n    };\n    ```\n* `withoutRouteHandler *(ctx, next, controller)` is a middleware function for handle the request without route.\n\n    So the context of the function is the current koa context.\n\n### Static Attribute\n\n`route.controller` is a freeze object like a tree. And the value must be a function at the end of the tree.\n\n## Controller\n\n* `index` is default path.\n\n    ```js\n    // File: app.js\n    \n    // GET /app\n    exports.index = function () {\n        this.body = 'app';\n    };\n    exports.index = function (id) {\n        this.body = id || 'app';\n    };\n    ```\n* Http method is a prefix of function name but `GET`.\n\n    ```js\n    // File: index.js\n    \n    // GET /\n    exports.index = function (id) {\n        this.body = id;\n    };\n    \n    // POST /\n    exports.postIndex = function (id) {\n        this.body = id;\n    };\n    \n    // PUT /\n    exports.putIndex = function (id) {\n        this.body = id;\n    };\n    \n    // DELETE /\n    exports.deleteIndex = function (id) {\n        this.body = id;\n    };\n    ```\n\n## Test\n\n```sh\n./node_modules/.bin/mocha\n# or\nnpm test # npm run test\n```\n\n## The End\n\nAnyway, try to use. And see the example in example directory.\n\nHope this is useful to you.\n\nAnd give me your pull request if you have any improvements or suggestions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyss%2Fkoa-route-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyss%2Fkoa-route-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyss%2Fkoa-route-tree/lists"}