Need help with route?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

koajs
439 Stars 45 Forks 78 Commits 6 Opened issues

Description

Simple route middleware

Services available

!
?

Need anything else?

Contributors list

No Data

koa-route

Uber simple route middleware for koa.

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, a Koa clone of express-resource.

Installation

$ npm install koa-route

Example

Contrived resource-oriented example:

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); console.log('listening on port 3000');

License

MIT

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.