koa-router

Router middleware for koa. Provides RESTful resource routing.

Please use @koa/router instead, starting from v9!
Last updated 9 years ago by jbielick .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install koa-router 
SYNC missed versions from official npm registry.

koa-router

NPM version NPM Downloads Node.js Version Build Status Gitter Chat

Express-like HTTP routing for koa

API Documentation

Examples and Other Documentation

Changelog

UPGRADING.md

Installation

koa-router requires node 8 or above.

npm install -S koa-router

Hello World

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

router.get('/hello', async (ctx) => {
  ctx.body = 'world';
});

app.use(router.routes());
app.listen(3000);

Features

  • Express-style routing using router.get, router.put, router.post, etc
const router = new Router();

router.patch('/users/:id', (ctx) => ...);
router.get('/users', (ctx) => ...);
  • Named URL parameters (/users/:id -> ctx.params.id)
const router = new Router();

router.get('/articles/:category', (ctx) => {
  // ctx.params.category available
});
  • Named routes with URL/path generation
const router = new Router();

router.get('article', '/article/:id', (ctx) => ...);
router.path('article', 3);
// => /articles/3
  • Nest, prefix routers recursively
const rootRouter = new Router();
const authorRouter = new Router();
const bookRouter = new Router();

bookRouter.get('/', (ctx) => ...);
authorRouter.get('/:id', (ctx) => ...);

authorRouter.nest('/:id/books', bookRouter);
rootRouter.nest('/books', bookRouter);
rootRouter.nest('/authors', authorRouter);

rootRouter.routes();
// =>
// /books
// /authors/:id
// /authors/:id/books

  • async/await support
const router = new Router();

router.get('/users/:id', async (ctx) => {
  ctx.state.user = await User.find(ctx.params.id);
});
  • Responds to OPTIONS requests with allowed methods
  • Support for 405 Method Not Allowed and 501 Not Implemented handlers
  • Robust matching engine: path-to-regexp

Koa Router version 7 and 8 are only compatible with koa 2 and do not work with koa 1 or any other koa 1 middlewares

Upgrading from 7.x to 8.x

See the UPGRADING.md

Contributing

Please submit all issues and pull requests to the alexmingoia/koa-router repository!

Generate coverage reports with npm run coverage

Generate the docs with npm run docs

Run the linter with npm lint

Tests

Run tests using npm test

Support

If you identify an issue with this software or have a suggestion, please open an issue here.

License

MIT

Current Tags

  • 14.0.0                                ...           latest (a year ago)
  • 8.0.0-alpha.1                                ...           next (9 years ago)

93 Versions

  • 14.0.0 [deprecated]           ...           a year ago
  • 13.1.1 [deprecated]           ...           a year ago
  • 13.0.1 [deprecated]           ...           2 years ago
  • 11.0.2 [deprecated]           ...           3 years ago
  • 12.0.1 [deprecated]           ...           3 years ago
  • 12.0.0 [deprecated]           ...           4 years ago
  • 11.0.1 [deprecated]           ...           4 years ago
  • 11.0.0 [deprecated]           ...           4 years ago
  • 10.1.1 [deprecated]           ...           5 years ago
  • 10.0.0 [deprecated]           ...           6 years ago
  • 9.4.0 [deprecated]           ...           6 years ago
  • 9.1.0 [deprecated]           ...           6 years ago
  • 9.0.1 [deprecated]           ...           7 years ago
  • 8.0.8 [deprecated]           ...           7 years ago
  • 8.0.7 [deprecated]           ...           7 years ago
  • 8.0.6 [deprecated]           ...           7 years ago
  • 8.0.0-alpha.1 [deprecated]           ...           9 years ago
  • 7.4.0 [deprecated]           ...           9 years ago
  • 7.3.0 [deprecated]           ...           9 years ago
  • 7.2.1 [deprecated]           ...           9 years ago
  • 5.4.2 [deprecated]           ...           10 years ago
  • 7.2.0 [deprecated]           ...           10 years ago
  • 7.1.1 [deprecated]           ...           10 years ago
  • 5.4.1 [deprecated]           ...           10 years ago
  • 7.1.0 [deprecated]           ...           10 years ago
  • 5.4.0 [deprecated]           ...           11 years ago
  • 7.0.1 [deprecated]           ...           11 years ago
  • 5.3.0 [deprecated]           ...           11 years ago
  • 7.0.0 [deprecated]           ...           11 years ago
  • 6.2.0 [deprecated]           ...           11 years ago
  • 6.1.0 [deprecated]           ...           11 years ago
  • 6.0.1 [deprecated]           ...           11 years ago
  • 6.0.0 [deprecated]           ...           11 years ago
  • 5.2.3 [deprecated]           ...           11 years ago
  • 5.2.2 [deprecated]           ...           11 years ago
  • 5.2.1 [deprecated]           ...           11 years ago
  • 5.2.0 [deprecated]           ...           11 years ago
  • 5.1.2 [deprecated]           ...           11 years ago
  • 5.1.1 [deprecated]           ...           11 years ago
  • 5.1.0 [deprecated]           ...           11 years ago
  • 5.0.1 [deprecated]           ...           12 years ago
  • 5.0.0 [deprecated]           ...           12 years ago
  • 4.3.2 [deprecated]           ...           12 years ago
  • 4.3.1 [deprecated]           ...           12 years ago
  • 4.3.0 [deprecated]           ...           12 years ago
  • 4.2.0 [deprecated]           ...           12 years ago
  • 4.1.0 [deprecated]           ...           12 years ago
  • 4.0.1 [deprecated]           ...           12 years ago
  • 4.0.0 [deprecated]           ...           12 years ago
  • 3.8.0 [deprecated]           ...           12 years ago
  • 3.7.0 [deprecated]           ...           12 years ago
  • 3.6.0 [deprecated]           ...           12 years ago
  • 3.5.0 [deprecated]           ...           12 years ago
  • 3.4.0 [deprecated]           ...           12 years ago
  • 3.3.1 [deprecated]           ...           12 years ago
  • 3.3.0 [deprecated]           ...           12 years ago
  • 3.2.3 [deprecated]           ...           12 years ago
  • 3.2.2 [deprecated]           ...           12 years ago
  • 3.2.1 [deprecated]           ...           12 years ago
  • 3.2.0 [deprecated]           ...           12 years ago
  • 3.1.4 [deprecated]           ...           13 years ago
  • 3.1.3 [deprecated]           ...           13 years ago
  • 3.1.2 [deprecated]           ...           13 years ago
  • 3.1.1 [deprecated]           ...           13 years ago
  • 3.1.0 [deprecated]           ...           13 years ago
  • 3.0.2 [deprecated]           ...           13 years ago
  • 3.0.1 [deprecated]           ...           13 years ago
  • 3.0.0 [deprecated]           ...           13 years ago
  • 2.4.4 [deprecated]           ...           13 years ago
  • 2.4.3 [deprecated]           ...           13 years ago
  • 2.3.3 [deprecated]           ...           13 years ago
  • 2.4.1 [deprecated]           ...           13 years ago
  • 2.3.2 [deprecated]           ...           13 years ago
  • 2.4.0 [deprecated]           ...           13 years ago
  • 2.3.1 [deprecated]           ...           13 years ago
  • 2.3.0 [deprecated]           ...           13 years ago
  • 2.2.0 [deprecated]           ...           13 years ago
  • 2.1.0 [deprecated]           ...           13 years ago
  • 2.0.1 [deprecated]           ...           13 years ago
  • 2.0.0 [deprecated]           ...           13 years ago
  • 1.6.1 [deprecated]           ...           13 years ago
  • 1.6.0 [deprecated]           ...           13 years ago
  • 1.5.5 [deprecated]           ...           13 years ago
  • 1.5.4 [deprecated]           ...           13 years ago
  • 1.5.3 [deprecated]           ...           13 years ago
  • 1.4.3 [deprecated]           ...           13 years ago
  • 1.3.3 [deprecated]           ...           13 years ago
  • 1.2.3 [deprecated]           ...           13 years ago
  • 1.2.2 [deprecated]           ...           13 years ago
  • 1.1.2 [deprecated]           ...           13 years ago
  • 1.1.1 [deprecated]           ...           13 years ago
  • 1.0.1 [deprecated]           ...           13 years ago
  • 1.0.0 [deprecated]           ...           13 years ago
Maintainers (2)
Downloads
Total 436
Today 0
This Week 0
This Month 213
Last Day 2
Last Week 25
Last Month 90
Dependencies (5)
Dev Dependencies (9)

© 2010 - cnpmjs.org x YWFE | Home | YWFE