koa-router-factory

1.2.0 • Public • Published

koa-router-factory

Factory for koa-router 7.x

  • Crate koa-router with config
  • Support nested routes

Installation

Install using npm:

npm install koa-router-factory

Example

Basic usage :

import Koa from "koa";
import routerFactory from "koa-router-factory";
 
const IndexController = {
  index: (async function () {
    this.body = "index action";
  }),
 
  secondAction: (async function () {
    this.body = "second action";
  })
}
 
const app = new Koa();
const router = routerFactory({
  index: {
    url: "/",
    controller: IndexController,
    childs: {
      second: {
        url: "/second",
        action: "secondAction"
      }
    }
  }
});
 
app.use(router.routes())
   .use(router.allowedMethods());

Config keys

Key Type Description
url String URL for the route
controller Object Object contains all actions needed. Is optional for child route. If not defined for child route, parent controller is used
action String If not specified, index action is used
childs Object Child routes definitions
method String get, post, put, delete, patch or all HTTP method. Default is get

Context is binded to tha action call.

Tests

Run test using npm test. Run coverage using npm run coverage.

Package Sidebar

Install

npm i koa-router-factory

Weekly Downloads

1

Version

1.2.0

License

MIT

Last publish

Collaborators

  • yannickda