Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/koajs/route
Simple route middleware
https://github.com/koajs/route
Last synced: 1 day ago
JSON representation
Simple route middleware
- Host: GitHub
- URL: https://github.com/koajs/route
- Owner: koajs
- Created: 2013-11-06T21:04:46.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2024-09-16T00:38:28.000Z (3 months ago)
- Last Synced: 2024-10-29T15:32:22.699Z (about 1 month ago)
- Language: JavaScript
- Size: 222 KB
- Stars: 446
- Watchers: 11
- Forks: 46
- Open Issues: 10
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
Awesome Lists containing this project
- awesome-koa - route - Simple route middleware (Middleware)
- awesome-koa - koa-route - 一个简单的路由中间件。 ![](https://img.shields.io/github/stars/koajs/route.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/koa-route.svg?style=flat-square) (仓库 / 中间件)
README
# koa-route
Uber simple route middleware for koa.
```js
const _ = require('koa-route');
app.use(_.get('/pets', pets.list));
app.use(_.get('/pets/:name', pets.show));
```If you need a full-featured solution check out [koa-router](https://github.com/koajs/router),
a Koa clone of express-resource.## Installation
```js
$ npm install koa-route
```## Example
Contrived resource-oriented example:
```js
const _ = require('koa-route');
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);
ctx.body = 'pets: ' + names.join(', ');
},show: (ctx, name) => {
const pet = db[name];
if (!pet) return ctx.throw('cannot find that pet', 404);
ctx.body = pet.name + ' is a ' + pet.species;
}
};app.use(_.get('/pets', pets.list));
app.use(_.get('/pets/:name', pets.show));app.listen(3000, (err) => {
if (err) console.error(err.stack);
else console.log('listening on port 3000');
});
```## License
MIT