ZijianHe/koa-router is an open-source project on GitHub with 4.8k stars, written primarily in JavaScript. Router middleware for koa.
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
Express-style routing using app.get, app.put, app.post, etc.
Named URL parameters.
Named routes with URL generation.
Responds to OPTIONS requests with allowed methods.
Support for 405 Method Not Allowed and 501 Not Implemented.
Multiple route middleware.
Multiple routers.
Nestable routers.
ES7 async/await support.
Migrating to 7 / Koa 2
The API has changed to match the new promise-based middleware
signature of koa 2. See the
koa 2.x readme for more
information.
Middleware is now always run in the order declared by .use() (or .get(),
etc.), which matches Express 4 API.
Installation
Install using npm:
npm install koa-router
API Reference
koa-router
Router ⏏
new Router([opts])
instance
.get|put|post|patch|delete|del ⇒ Router
.routes ⇒ function
.use([path], middleware) ⇒ Router
.prefix(prefix) ⇒ Router
.allowedMethods([options]) ⇒ function
.redirect(source, destination, [code]) ⇒ Router
.route(name) ⇒ Layer | false
.url(name, params, [options]) ⇒ String | Error
.param(param, middleware) ⇒ Router
static
.url(path, params) ⇒ String
Router ⏏
Kind: Exported class
new Router([opts])
Create a new router.
Param
Type
Description
[opts]
Object
[opts.prefix]
String
prefix router paths
Example
Basic usage:
var Koa = require('koa');
var Router = require('koa-router');
var app = new Koa();
var router = new Router();
router.get('/', (ctx, next) => {
// ctx.router available
});
app
.use(router.routes())
.use(router.allowedMethods());
router.get|put|post|patch|delete|del ⇒ Router
Create router.verb() methods, where verb is one of the HTTP verbs such
as router.get() or router.post().
Match URL patterns to callback functions or controller actions using router.verb(),
where verb is one of the HTTP verbs such as router.get() or router.post().
Additionaly, router.all() can be used to match against all methods.
var forums = new Router();
var posts = new Router();
posts.get('/', (ctx, next) => {...});
posts.get('/:pid', (ctx, next) => {...});
forums.use('/forums/:fid/posts', posts.routes(), posts.allowedMethods());
// responds to "/forums/123/posts" and "/forums/123/posts/123"
app.use(forums.routes());
Router prefixes
Route paths can be prefixed at the router level:
var router = new Router({
prefix: '/users'
});
router.get('/', ...); // responds to "/users"
router.get('/:id', ...); // responds to "/users/:id"
URL parameters
Named route parameters are captured and added to ctx.params.
The path-to-regexp module is
used to convert paths to regular expressions.
Kind: instance property of Router
Param
Type
Description
path
String
[middleware]
function
route middleware(s)
callback
function
route callback
router.routes ⇒ function
Returns router middleware which dispatches a route matching the request.
Kind: instance property of Router
router.use([path], middleware) ⇒ Router
Use given middleware.
Middleware run in the order they are defined by .use(). They are invoked
sequentially, requests start at the first middleware and work their way
"down" the middleware stack.
Kind: instance method of Router
Param
Type
[path]
String
middleware
function
[...]
function
Example
// session middleware will run before authorize
router
.use(session())
.use(authorize());
// use middleware only with given path
router.use('/users', userAuth());
// or with an array of paths
router.use(['/users', '/admin'], userAuth());
app.use(router.routes());
router.prefix(prefix) ⇒ Router
Set the path prefix for a Router instance that was already initialized.
Kind: instance method of Router
Param
Type
prefix
String
Example
router.prefix('/things/:thing_id')
router.allowedMethods([options]) ⇒ function
Returns separate middleware for responding to OPTIONS requests with
an Allow header containing the allowed methods, as well as responding
with 405 Method Not Allowed and 501 Not Implemented as appropriate.
Kind: instance method of Router
Param
Type
Description
[options]
Object
[options.throw]
Boolean
throw error instead of setting status and header
[options.notImplemented]
function
throw the returned value in place of the default NotImplemented error
[options.methodNotAllowed]
function
throw the returned value in place of the default MethodNotAllowed error
Example
var Koa = require('koa');
var Router = require('koa-router');
var app = new Koa();
var router = new Router();
app.use(router.routes());
app.use(router.allowedMethods());
Example with Boom
var Koa = require('koa');
var Router = require('koa-router');
var Boom = require('boom');
var app = new Koa();
var router = new Router();
app.use(router.routes());
app.use(router.allowedMethods({
throw: true,
notImplemented: () => new Boom.notImplemented(),
methodNotAllowed: () => new Boom.methodNotAllowed()
}));
No homepage URL was recorded for ZijianHe/koa-router in TopGit's last sync. The README tab above frequently contains screenshots and demo links, or check the repository description on GitHub.
How many stars does ZijianHe/koa-router have?
ZijianHe/koa-router has 4.8k GitHub stars — refresh the page for the live number, or check github.com/ZijianHe/koa-router. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
Is ZijianHe/koa-router open source?
Yes — ZijianHe/koa-router ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/ZijianHe/koa-router.
What is ZijianHe/koa-router?
ZijianHe/koa-router (ZijianHe/koa-router) is a JavaScript project on GitHub. From the project's own README: Router middleware for koa.
Where do I read more about ZijianHe/koa-router?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/ZijianHe/koa-router is the definitive source.
Read full README in the tab above.
Is koa-router worth your time?
ChatGPT, Claude and Perplexity can all read this page. Ask one of them what it makes of koa-router.