1.0.1 • Published 5 years ago
@ostai/koa-response v1.0.1
@ostai/koa-response
Response middleware for koa that will handle response body and errors.
This module is designed to standardize server response structure.
Install
$ npm i @ostai/koa-responseUsage
const Koa = require('koa')
const Router = require('@koa/router')
const response = require('@ostai/koa-response')
const app = new Koa()
const router = new Router()
router.get('/foo', () => 'ok')
router.get('/bar', () => {
const error = new Error('bar')
error.status = 401
throw error
})
app
.use(
response({
debug: true
})
)
.use(router.routes())
.use(router.allowedMethods())
app.listen(8888)> curl http://localhost:8888/foo
# http 200
# ok> curl http://localhost:8888/bar
# http 401
# {"message":"bar"}response(options?): Function
- options?
Object- error?
Function(ctx, error, rest): voidthe method to handle error - success?
Function(ctx, body, rest): voidthe method to handle success - ...rest?
Object- debug
boolean=falseWhich is used by the default value ofoptions.error. By default, ifdebugisfalse, error response will not containmessage.
- debug
- error?
Returns Function the middleware function.