This module provides a set of koa
middleware and optional CRUD resource(s). Middleware include:
koa-response-time
koa-helmet
@koa/cors
koa-compress
koa-conditional-get
koa-etag
koa-respond
koa-body
koa-pino-logger
swagger2-koa
Install using npm:
npm install johnschult/koa-stark
const Koa = require('koa')
const stark = require('koa-stark')
const app = new Koa()
app.use(stark(options, app))
app.listen(9000)
The above creates a koa
server with koa-stark
middleware, where:
options
- optional, object where:basePath
- optional, a base path to serve requests from, e.g.'/api'
mongo
- required ifswagger.spec
includes paths that require CRUD, a standard MongoDB connection stringlogging
- optional, if set totrue
will usekoa-pino-logger
swagger
- optional, an object where:validate
- optional, if set totrue
will validate requests and responses usingswagger2-koa.validate
ui
- optional, if set totrue
will serve a Swagger UI atoptions.basePath
usingswagger2-koa.ui
resources
- optional, an array of objects where:name
- required, the name the resource, e.g.'Robot'
path
- required, the path to the resource, e.g.'/v1/clients'
mongooseSchema
- optional, the resource schema, a mongoose.Schema object, if provided, a CRUD controller and routes will be generated for this resourceswaggerSchema
- required, an OpenAPI 2.0 Definitions Object
If resources
is defined in options
, the following routes are defined for each resource:
GET /<basePath>/<path>/
- Returns an array of all the modelsPOST /<basePath>/<path>/
- Creates a new modelGET /<basePath>/<path>/:id
- Finds a model by IDPUT /<basePath>/<path>/:id
- Updates an existing modelDELETE /<basePath>/<path>/:id
- Deletes a modelGET /<basePath>/<path>/:id/exists
- Checks whether a model existsGET /<basePath>/<path>/count
- Returns a count of all the models
A route is added using socket.io to enable watching individual or all
model instance(s). In order to utilize the watch
route, a socket.io connection needs
to be made. For example:
const io = require('socket.io-client')
const socket = io('ws://localhost:9006/api/v1/robots/watch')
watch
- Used to define the resources and operations you are interested in observing, optional object where:id
- optional, the resourceid
operationType
- optional, any ofinsert, update, delete
as a comma separated string
change
- The server emits changes to this event. A single argument is sent to the callback containing the change data.
Watch all operations on all resource instances:
const io = require('socket.io-client')
const socket = io('ws://localhost:9006/api/v1/robots/watch')
socket.on('connect', () => {
socket.emit('watch')
})
socket.on('change', data => {
console.log(data)
})
socket.on('disconnect', () => {
process.exit()
})
Watch all update operation on a single resource instances and disconnect after first change is emitted:
const io = require('socket.io-client')
const socket = io('ws://localhost:9006/api/v1/robots/watch')
socket.on('connect', () => {
socket.emit('watch', {
operationType: 'update',
id: '5af2fe000e488a70acf4be22',
disconnect: true
})
})
socket.on('change', data => {
console.log(data)
})
socket.on('disconnect', () => {
process.exit()
})
A simple koa
application is included in the example
directory.
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.
To run this example:
$ git clone https://github.com/johnschult/koa-stark
$ cd koa-stark && npm install
$ npm run mongod
$ npm run example
Browse to http://localhost:9006/api to use the example OpenAPI 2.0 UI.