{"id":13799719,"url":"https://github.com/nervgh/koa-architect","last_synced_at":"2025-04-15T08:59:06.010Z","repository":{"id":143882680,"uuid":"90367032","full_name":"nervgh/koa-architect","owner":"nervgh","description":"Automates mounting and routing","archived":false,"fork":false,"pushed_at":"2017-10-05T14:55:51.000Z","size":21,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-28T18:21:18.152Z","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/nervgh.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-05T10:58:35.000Z","updated_at":"2023-09-08T10:07:31.000Z","dependencies_parsed_at":null,"dependency_job_id":"be943047-d4de-470c-9e76-4693039748e8","html_url":"https://github.com/nervgh/koa-architect","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nervgh%2Fkoa-architect","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nervgh%2Fkoa-architect/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nervgh%2Fkoa-architect/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nervgh%2Fkoa-architect/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nervgh","download_url":"https://codeload.github.com/nervgh/koa-architect/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248830417,"owners_count":21168275,"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-04T00:01:05.413Z","updated_at":"2025-04-15T08:59:05.994Z","avatar_url":"https://github.com/nervgh.png","language":"JavaScript","funding_links":[],"categories":["仓库"],"sub_categories":["中间件"],"readme":"# koa-architect\n\n[![NPM version][npm-image]][npm-url]\n[![Build status][travis-image]][travis-url]\n[![Test coverage][coveralls-image]][coveralls-url]\n\n\n## About\nReads a folder with middleware and routes and reduces them to middleware using [koa-mount](https://github.com/koajs/mount) and [koa-trie-router](https://github.com/koajs/trie-router).  \nThat eventually allows you run your app extremely simple:\n\n```js\nconst Koa = require('koa')\nconst architect = require('koa-architect')\n\nlet app = new Koa()\n\nfor(let middleware of architect.readMiddlewareAndRoutes('./middleware')) {\n  app.use(middleware)\n}\n```\nAll of you need is keep in mind next things:\n1. each your file should returns a middleware or routes\n2. if it returns middleware then it will be pushed to the middleware stack\n3. if it returns routes then they will be reduced to middleware using a router\n\n### How does exactly your routes will be reduced to middleware?\n```js\n// router.use(fn)\nexports.use = function (ctx) {\n}\n\n// router.get('/', fn)\nexports.get = {\n  'index': function (ctx) {\n  }\n}\n\n// router.post('/foo', fn)\n// router.post('/bar/:id', fn)\nexports.post = {\n  'foo': function (ctx) {\n  },\n  'bar/:id': function (ctx) {\n  }\n}\n```\n\n## Example\nAssume, we have next folder tree and code:\n\n![example](./screenshot.png)\n\n```\nmiddleware/\nmiddleware/01-foo/index.js\nmiddleware/02-bar/index.js\nmiddleware/route/index.js\nmiddleware/nested/index.js\nmiddleware/nested/middleware/index/index.js\nmiddleware/nested/middleware/baz/index.js\n```\n**middleware/01-foo/index.js**\n```js\n// Middleware\nmodule.exports = function (ctx, next) {\n  // Doing here some work and go next\n  ctx.state.foo = 1\n  return next()\n}\n```\n**middleware/02-bar/index.js**\n```js\n// Middleware\nmodule.exports = function (ctx, next) {\n  // Doing here some work and go next\n  ctx.state.bar = 1\n  return next()\n}\n```\n**middleware/route/index.js**\n```js\n// Routes\nexports.get = {\n  'index': function (ctx) {\n    ctx.response.body = ctx.state\n  }\n}\n\nexports.post = {\n  'test/:id': function (ctx) {\n    ctx.response.body = ctx.params.id\n  }\n}\n```\n**middleware/nested/index.js**\n```js\n// Nested middleware and/or routes\n\nconst path = require('path')\nconst architect = require('koa-architect')\n\n// Since this folder contains nested middleware/routes\n// we need use koa-architect to read them\n\nexports.use = architect.readMiddlewareAndRoutes(path.join(__dirname, './middleware'))\n```\n**middleware/nested/middleware/index/index.js**\n```js\n// Routes\nexports.use = function (ctx) {\n  ctx.response.body = ctx.originalUrl\n}\n```\n**middleware/nested/middleware/baz/index.js**\n```js\n// Routes\nexports.get = {\n  'index': function (ctx) {\n    ctx.response.body = ctx.originalUrl\n  }\n}\n```\n\nAnd we launch our app using this way:\n```js\nconst Koa = require('koa')\nconst architect = require('koa-architect')\n\nlet app = new Koa()\n\nfor(let middleware of architect.readMiddlewareAndRoutes('./middleware')) {\n  app.use(middleware)\n}\n```\n\nThus we will get a server which handle requests next way:\n```\nGET --\u003e / \nGET \u003c-- 404 \"Not Found\"\n\nGET --\u003e /route\nGET \u003c-- 200 {\"foo\":1,\"bar\":1}\n\nPOST --\u003e /route/test/1\nPOST \u003c-- 200 \"1\"\n\nGET --\u003e /nested\nGET \u003c-- 200 \"/nested\"\n\nGET --\u003e /nested/baz\nGET \u003c-- 200 \"/nested/baz\"\n```\n\nSee [test/fixtures](./test/fixtures) for details.\n\n\n## Package managers\n### NPM\n```\nnpm install koa-architect\n```\nYou could find this module in npm like [_koa-architect_](https://www.npmjs.com/search?q=koa-architect).\n\n\n[npm-image]: https://img.shields.io/npm/v/koa-architect.svg?style=flat\n[npm-url]: https://npmjs.org/package/koa-architect\n[travis-image]: https://img.shields.io/travis/nervgh/koa-architect.svg?style=flat\n[travis-url]: https://travis-ci.org/nervgh/koa-architect\n[coveralls-image]: https://img.shields.io/coveralls/nervgh/koa-architect.svg?style=flat\n[coveralls-url]: https://coveralls.io/r/nervgh/koa-architect?branch=master","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnervgh%2Fkoa-architect","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnervgh%2Fkoa-architect","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnervgh%2Fkoa-architect/lists"}