
Product
Introducing Reports: An Extensible Reporting Framework for Socket Data
Explore exportable charts for vulnerabilities, dependencies, and usage with Reports, Socket’s new extensible reporting framework.
Modular Koa router middleware with Express-style routes and middleware mounting
Modular Koa router middleware with Express-style routes and middleware mounting.
npm install kroute --save
Kroute exports a function which can be used to create router instances that work with Koa middleware.
var koa = require('koa')
var kroute = require('kroute')
var app = koa()
var router = kroute()
app.use(router)
A router can be initialized with default handlers based on a resourceful routing object.
router({
use: authorizeUser(),
index: function* () {},
create: [function* () {}, function* () {}]
})
Actions are mapped as follows:
GET / -> index
GET /new -> new
POST / -> create
GET /:id -> show
GET /:id/edit -> edit
PUT /:id -> update
DELETE /:id -> destroy
The object can also contain a use property which is mounted before any routes.
Every router instance can used to attach request handlers.
router.get('/user', function* () {})
Omitting the path name will ensure the handler is always executed when the method matches.
router.post(function* () {})
Every route method accepts multiple middleware handlers.
router.delete(authorizeUser(), function* () {})
Every router provides a .all method for attaching request handlers to every method.
router.all(function* () {})
It accepts a path and multiple middleware like any other method.
router.all('/', authorizeUser(), function* () {})
Every router comes with the ability to mount middleware and handlers.
var mount = kroute()
router.use('/users', mount)
As long as the middleware follows the Koa generator pattern, it can be mounted.
router.use('/users', function* (next) {
console.log(this.url) //=> "/123" -> Stripped the route prefix.
yield next
})
Like other methods, .use also accepts multiple request handlers and an optional path.
router.use(authorizeUser(), function* () {})
Every path can be dynamic and use Express-style parameter notation.
router.get('/:user', function* () {
console.log(this.params.user) //=> "123"
})
Every match is stored in the params as an object.
router.get('/:foo/:bar', function* () {
console.log(this.params) //=> { foo: "123", bar: "456" }
})
The route can also be a regular expression.
router.get(/^\/blog\/(\d{4})-(\d{2})-(\d{2})\/?$/i, function* (next) {
console.log(this.params) // => { 0: '2014', 1: '03', 2: '17' }
})
Every method returns it's own instance, so routes can be chained together like Express.
router
.get('/foo', function* () {})
.post('/bar', function* () {})
Every method accepts an options object as the last argument.
router.get('/foo', function* () {}, { strict: true, sensitive: true })
MIT
FAQs
Modular Koa router middleware with Express-style routes and middleware mounting
We found that kroute demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Explore exportable charts for vulnerabilities, dependencies, and usage with Reports, Socket’s new extensible reporting framework.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.