Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/potatogopher/koa-response-handler

Lightweight response handler for Koa JS
https://github.com/potatogopher/koa-response-handler

es6 javascript koa-router koajs nodejs

Last synced: about 1 month ago
JSON representation

Lightweight response handler for Koa JS

Awesome Lists containing this project

README

        

# koa-response-handler
Response handler middleware for [koa](https://github.com/koajs/koa).

![KoaJs Slack](https://img.shields.io/badge/Koa.Js-Slack%20Channel-Slack.svg?longCache=true&style=for-the-badge)

## Installation
```js
$ yarn add koa-response-handler
```
## Example
```js
import Koa from 'koa'
import Router from 'koa-router'
import responseHandler from 'koa-response-handler'

const app = new Koa()
const router = new Router()

app
.use(responseHandler({ contentType: 'application/json' }))
.use(router.routes())
.use(router.allowedMethods())

router.get('/:id', function async (ctx, next) {
let data = {id: 1, hello: 'world' }

if (ctx.params.id !== data.id) {
ctx.response.notFound()
return
}

ctx.response.ok(data)
})
```

## License
MIT