https://github.com/bttmly/koa-route-respond
`response-objects`-compatible routing middleware for Koa v2
https://github.com/bttmly/koa-route-respond
Last synced: 3 months ago
JSON representation
`response-objects`-compatible routing middleware for Koa v2
- Host: GitHub
- URL: https://github.com/bttmly/koa-route-respond
- Owner: bttmly
- Created: 2017-02-15T02:28:40.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-02-15T02:42:19.000Z (about 8 years ago)
- Last Synced: 2025-01-08T03:44:13.370Z (4 months ago)
- Language: JavaScript
- Size: 43 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README
# koa-route-respond
Route middleware for Koa with [response object](https://github.com/nickb1080/response-objects) support. Fork of [koa-route](https://github.com/koajs/route).
```js
const r = require('koa-route-respond');
app.use(r.get('/pets', pets.list));
app.use(r.get('/pets/:name', pets.show));
```## Installation
```
$ npm install koa-route-respond
```## Example
Contrived resource-oriented example:
```js
const r = require('koa-route');
const { Ok, NotFound } = require('response-objects');
const Koa = require('koa');
const app = new Koa();const db = {
tobi: { name: 'tobi', species: 'ferret' },
loki: { name: 'loki', species: 'ferret' },
jane: { name: 'jane', species: 'ferret' },
};const pets = {
list (ctx) {
const names = Object.keys(db);
return Ok(`pets: ${names.join(', ')}`);
},show (ctx, {name}) {
const pet = db[name];
if (!pet) throw NotFound('cannot find that pet');
return Ok(`${pet.name} is a ${pet.species}`);
},
};app.use(r.get('/pets', pets.list));
app.use(r.get('/pets/:name', pets.show));
app.listen(3000);
```## License
MIT