{"id":19110425,"url":"https://github.com/johnschult/koa-stark","last_synced_at":"2026-05-17T02:05:36.124Z","repository":{"id":66524969,"uuid":"130865471","full_name":"johnschult/koa-stark","owner":"johnschult","description":"Provides a set of `koa` middleware and optional CRUD resource(s) using `mongoose`","archived":false,"fork":false,"pushed_at":"2018-06-07T19:10:21.000Z","size":119,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-03T04:16:39.302Z","etag":null,"topics":["koa"],"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/johnschult.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-04-24T14:17:10.000Z","updated_at":"2018-06-07T19:10:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"4fe9e8be-d2f6-47c7-bdd7-776f4bcad11b","html_url":"https://github.com/johnschult/koa-stark","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnschult%2Fkoa-stark","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnschult%2Fkoa-stark/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnschult%2Fkoa-stark/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnschult%2Fkoa-stark/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/johnschult","download_url":"https://codeload.github.com/johnschult/koa-stark/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240164480,"owners_count":19758177,"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":["koa"],"created_at":"2024-11-09T04:24:59.197Z","updated_at":"2025-10-27T06:17:46.168Z","avatar_url":"https://github.com/johnschult.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# koa-stark\n\nThis module provides a set of `koa` middleware and optional CRUD resource(s). Middleware include:\n\n* `koa-response-time`\n* `koa-helmet`\n* `@koa/cors`\n* `koa-compress`\n* `koa-conditional-get`\n* `koa-etag`\n* `koa-respond`\n* `koa-body`\n* `koa-pino-logger`\n* `swagger2-koa`\n\n## Installation\n\nInstall using [npm](https://www.npmjs.org/):\n\n```\nnpm install johnschult/koa-stark\n```\n\n## Usage\n\n```javascript\nconst Koa = require('koa')\nconst stark = require('koa-stark')\nconst app = new Koa()\n\napp.use(stark(options, app))\n\napp.listen(9000)\n```\n\nThe above creates a `koa` server with `koa-stark` middleware, where:\n\n* `options` - _optional_, object where:\n  * `basePath` - _optional_, a base path to serve requests from, e.g. `'/api'`\n  * `mongo` - _required if `swagger.spec` includes paths that require CRUD_, a [standard MongoDB connection string](https://docs.mongodb.com/manual/reference/connection-string/#standard-connection-string-format)\n  * `logging` - _optional_, if set to `true` will use `koa-pino-logger`\n  * `swagger` - _optional_, an object where:\n    * `validate` - _optional_, if set to `true` will validate requests and responses using `swagger2-koa.validate`\n    * `ui` - _optional_, if set to `true` will serve a [Swagger UI](https://swagger.io/swagger-ui/) at `options.basePath` using `swagger2-koa.ui`\n  * `resources` - _optional_, an array of objects where:\n    * `name` - _required_, the name the resource, e.g. `'Robot'`\n    * `path` - _required_, the path to the resource, e.g. `'/v1/clients'`\n    * `mongooseSchema` - _optional_, the resource schema, a [mongoose.Schema](http://mongoosejs.com/docs/api.html#schema_Schema) object, if provided, a CRUD controller and routes will be generated for this resource\n    * `swaggerSchema` - _required_, an [OpenAPI 2.0 Definitions Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#definitions-object)\n\n## Resources\n\nIf `resources` is defined in `options`, the following routes are defined for each resource:\n\n* `GET /\u003cbasePath\u003e/\u003cpath\u003e/` - Returns an array of all the models\n* `POST /\u003cbasePath\u003e/\u003cpath\u003e/` - Creates a new model\n* `GET /\u003cbasePath\u003e/\u003cpath\u003e/:id` - Finds a model by ID\n* `PUT /\u003cbasePath\u003e/\u003cpath\u003e/:id` - Updates an existing model\n* `DELETE /\u003cbasePath\u003e/\u003cpath\u003e/:id` - Deletes a model\n* `GET /\u003cbasePath\u003e/\u003cpath\u003e/:id/exists` - Checks whether a model exists\n* `GET /\u003cbasePath\u003e/\u003cpath\u003e/count` - Returns a count of all the models\n\n### Watching Resources\n\nA route is added using [socket.io](http://socket.io) to enable watching individual or all\nmodel instance(s). In order to utilize the `watch` route, a [socket.io](http://socket.io) connection needs\nto be made. For example:\n\n```javascript\nconst io = require('socket.io-client')\n\nconst socket = io('ws://localhost:9006/api/v1/robots/watch')\n```\n\n#### Events\n\n##### Client\n\n* `watch` - Used to define the resources and operations you are interested\n  in observing, _optional_ object where:\n  * `id` - _optional_, the resource `id`\n  * `operationType` - _optional_, any of `insert, update, delete` as a comma separated string\n\n##### Server\n\n* `change` - The server emits changes to this event. A single argument is sent to the callback containing the change data.\n\n##### Examples\n\n**Watch all operations on all resource instances:**\n\n```javascript\nconst io = require('socket.io-client')\n\nconst socket = io('ws://localhost:9006/api/v1/robots/watch')\n\nsocket.on('connect', () =\u003e {\n  socket.emit('watch')\n})\n\nsocket.on('change', data =\u003e {\n  console.log(data)\n})\n\nsocket.on('disconnect', () =\u003e {\n  process.exit()\n})\n```\n\n**Watch all update operation on a single resource instances and disconnect after first change is emitted:**\n\n```javascript\nconst io = require('socket.io-client')\n\nconst socket = io('ws://localhost:9006/api/v1/robots/watch')\n\nsocket.on('connect', () =\u003e {\n  socket.emit('watch', {\n    operationType: 'update',\n    id: '5af2fe000e488a70acf4be22',\n    disconnect: true\n  })\n})\n\nsocket.on('change', data =\u003e {\n  console.log(data)\n})\n\nsocket.on('disconnect', () =\u003e {\n  process.exit()\n})\n```\n\n## Example\n\nA simple `koa` application is included in the `example` directory.\n\n`koa-stark` is opinionated about the database being used and requires that you use MongoDB. Before running the example make changes to `mongo` in `example/config.js` as needed.\n\nTo run this example:\n\n```shell\n$ git clone https://github.com/johnschult/koa-stark\n$ cd koa-stark \u0026\u0026 npm install\n$ npm run mongod\n$ npm run example\n```\n\nBrowse to http://localhost:9006/api to use the example OpenAPI 2.0 UI.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnschult%2Fkoa-stark","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjohnschult%2Fkoa-stark","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnschult%2Fkoa-stark/lists"}