{"id":20542208,"url":"https://github.com/weivea/koa-service","last_synced_at":"2025-04-14T09:12:32.254Z","repository":{"id":83302361,"uuid":"126304234","full_name":"weivea/koa-service","owner":"weivea","description":"koa-service 就是在复杂业务场景下用于做业务逻辑封装的一个抽象层, 保持业务逻辑的独立性，抽象出来的 Service 可以被多个 Controller 重复调用","archived":false,"fork":false,"pushed_at":"2018-03-25T08:34:36.000Z","size":7,"stargazers_count":7,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-14T09:11:57.327Z","etag":null,"topics":[],"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/weivea.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":"2018-03-22T08:32:46.000Z","updated_at":"2022-05-01T03:55:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"e527d495-0e09-4b9b-8465-a2fe8a7ade72","html_url":"https://github.com/weivea/koa-service","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/weivea%2Fkoa-service","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weivea%2Fkoa-service/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weivea%2Fkoa-service/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weivea%2Fkoa-service/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/weivea","download_url":"https://codeload.github.com/weivea/koa-service/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248852181,"owners_count":21171842,"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-11-16T01:29:48.086Z","updated_at":"2025-04-14T09:12:32.246Z","avatar_url":"https://github.com/weivea.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# koa-servise\n\nkoa-router \b提供了control。  \nkoa-servise 为control提供service，  \n用法类似 egg 的service,    \n就不用再把臃肿的代码写在 control里边了\n\nservice下的方法 可以通过`this.ctx.servics.xxx` 相互调用，  \n`this.ctx`即为每次请求的ctx\n\n支持`async/await`方法\n\n\n## install\n\n```\nnpm install koa-service --save\n```\n\n## usage\n具体使用参考 [example](./example)\n\n```\n#目录环境\n./index.js\n./service\n./service/testa\n./service/testa/testd.js\n./service/testa.js\n./service/testb.js\n./service/testc\n./service/testc/testc.js\n```\n\n ./index.js\n```javascript\n\nconst path = require('path')\nconst Koa = require('koa')\nconst Router = require('koa-router')\nconst service = require('../lib/koaService')\n\nconst app = new Koa();\n\napp.use(service({\n  serviceRoot: path.join(__dirname, 'service')\n}))\n\nconst router = new Router();\nrouter.get('/', async function(ctx, next) {\n  ctx.service.testb.bFun()\n\n  const re = await ctx.service.testa.testd.b2Fun()\n  // console.log(re);\n  ctx.body = re\n  await next()\n});\napp\n  .use(router.routes())\n  .use(router.allowedMethods());\n\napp.listen(8081, () =\u003e {\n  console.log('listen 8081')\n})\n```\n\n写一个service文件 ./service/testb.js\n```javascript\nclass Servic {\n  bFun() {\n    console.log(__filename);\n    return __filename;\n  }\n  async b2Fun() {\n    const re = await this.ctx.service.testa.testd.b2Fun()\n    console.log(__filename, 'log out:' , re)\n    return await (() =\u003e\n      new Promise((resolve, reject) =\u003e {\n        // 自运行返回Promise\n        setTimeout(() =\u003e {\n          resolve(`async ${__filename}`);\n        }, 100);\n      }))();\n  }\n}\n\nmodule.exports = Servic;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweivea%2Fkoa-service","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fweivea%2Fkoa-service","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweivea%2Fkoa-service/lists"}