{"id":13678014,"url":"https://github.com/TencentCloudBase/tcb-router","last_synced_at":"2025-04-29T12:32:52.312Z","repository":{"id":32849883,"uuid":"142388792","full_name":"TencentCloudBase/tcb-router","owner":"TencentCloudBase","description":"小程序·云开发 云函数类 koa 路由工具","archived":true,"fork":false,"pushed_at":"2023-01-06T01:35:28.000Z","size":1332,"stargazers_count":385,"open_issues_count":13,"forks_count":32,"subscribers_count":14,"default_branch":"master","last_synced_at":"2024-10-02T14:38:52.127Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TencentCloudBase.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-07-26T04:30:04.000Z","updated_at":"2024-07-25T16:27:12.000Z","dependencies_parsed_at":"2023-01-14T22:25:52.976Z","dependency_job_id":null,"html_url":"https://github.com/TencentCloudBase/tcb-router","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TencentCloudBase%2Ftcb-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TencentCloudBase%2Ftcb-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TencentCloudBase%2Ftcb-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TencentCloudBase%2Ftcb-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TencentCloudBase","download_url":"https://codeload.github.com/TencentCloudBase/tcb-router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223923926,"owners_count":17225977,"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","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":[],"created_at":"2024-08-02T13:00:49.363Z","updated_at":"2024-11-11T20:30:49.762Z","avatar_url":"https://github.com/TencentCloudBase.png","language":"JavaScript","funding_links":[],"categories":["Library"],"sub_categories":["付费课程"],"readme":"# tcb-router\n\n[![NPM version][npm-image]][npm-url]\n[![Build Status](https://travis-ci.org/TencentCloudBase/tcb-router.svg?branch=master)](https://travis-ci.org/TencentCloudBase/tcb-router)\n[![npm download][download-image]][download-url]\n[![npm license][license-image]][download-url]\n[![Coverage Status](https://coveralls.io/repos/github/TencentCloudBase/tcb-router/badge.svg?branch=master)](https://coveralls.io/github/TencentCloudBase/tcb-router?branch=master)\n\n[npm-image]: https://img.shields.io/npm/v/tcb-router.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/tcb-router\n[david-image]: https://img.shields.io/david/673800357/tcb-router.svg?style=flat-square\n[download-image]: https://img.shields.io/npm/dm/tcb-router.svg?style=flat-square\n[download-url]: https://npmjs.org/package/tcb-router\n[license-image]: https://img.shields.io/npm/l/tcb-router.svg\n\n\u003e 基于 koa 风格的小程序·云开发云函数轻量级类路由库，主要用于优化服务端函数处理逻辑\n\n## 云函数端\n\n使用\n\n```bash\nnpm install --save tcb-router\n```\n\n```javascript\n// 云函数的 index.js\nconst TcbRouter = require('./router');\n\nexports.main = (event, context) =\u003e {\n    const app = new TcbRouter({ event });\n  \n    // app.use 表示该中间件会适用于所有的路由\n    app.use(async (ctx, next) =\u003e {\n        ctx.data = {};\n        await next(); // 执行下一中间件\n    });\n\n    // 路由为数组表示，该中间件适用于 user 和 timer 两个路由\n    app.router(['user', 'timer'], async (ctx, next) =\u003e {\n        ctx.data.company = 'Tencent';\n        await next(); // 执行下一中间件\n    });\n\n    // 路由为字符串，该中间件只适用于 user 路由\n    app.router('user', async (ctx, next) =\u003e {\n        ctx.data.name = 'heyli';\n        await next(); // 执行下一中间件\n    }, async (ctx, next) =\u003e {\n        ctx.data.sex = 'male';\n        await next(); // 执行下一中间件\n    }, async (ctx) =\u003e {\n        ctx.data.city = 'Foshan';\n        // ctx.body 返回数据到小程序端\n        ctx.body = { code: 0, data: ctx.data};\n    });\n\n    // 路由为字符串，该中间件只适用于 timer 路由\n    app.router('timer', async (ctx, next) =\u003e {\n        ctx.data.name = 'flytam';\n        await next(); // 执行下一中间件\n    }, async (ctx, next) =\u003e {\n        ctx.data.sex = await new Promise(resolve =\u003e {\n        // 等待500ms，再执行下一中间件\n        setTimeout(() =\u003e {\n            resolve('male');\n        }, 500);\n        });\n        await next(); // 执行下一中间件\n    }, async (ctx)=\u003e  {\n        ctx.data.city = 'Taishan';\n\n        // ctx.body 返回数据到小程序端\n        ctx.body = { code: 0, data: ctx.data };\n    });\n\n    return app.serve();\n\n}\n\n```\n\ntips: 小程序云函数的 node 环境默认支持 async/await 语法，推荐涉及到的异步操作时像 demo 中那样使用\n\n\n## 小程序端\n\n```javascript\n// 调用名为 router 的云函数，路由名为 user\nwx.cloud.callFunction({\n    // 要调用的云函数名称\n    name: \"router\",\n    // 传递给云函数的参数\n    data: {\n        $url: \"user\", // 要调用的路由的路径，传入准确路径或者通配符*\n        other: \"xxx\"\n    }\n});\n```\n\n## 接口\n\n### 构造函数\n* 参数\n    - Object，存入云函数的 `event` 参数\n\n* 示例\n```js\nconst TcbRouter = require('./router');\nexports.main = (event, context) =\u003e {\n    const app = new TcbRouter({ event });\n};\n```\n\n### app.use\n* 参数\n    - Function, 应用于所有路由的中间件\n\n* 示例\n```js\napp.use(async (ctx, next) =\u003e {\n    ctx.data = {};\n    await next(); // 执行下一中间件\n});\n```\n\n### app.router\n* 参数\n    - Array|String，路由或路由数组\n    - Function, 应用于对应路由的中间伯\n\n* 示例\n```js\napp.router(['user', 'timer'], async (ctx, next) =\u003e {\n    ctx.data = {}；\n    ctx.data.company = 'Tencent';\n    await next(); // 执行下一中间件\n});\n\n// 路由为字符串，该中间件只适用于 user 路由\napp.router('user', async (ctx, next) =\u003e {\n    ctx.data.name = 'heyli';\n    ctx.body = {code: 0, data: ctx.data}; // 将数据返回给云函数，用ctx.body\n});\n```\n\n## 测试\n\n```bash\nnpm run test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTencentCloudBase%2Ftcb-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTencentCloudBase%2Ftcb-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTencentCloudBase%2Ftcb-router/lists"}