1.2.0 • Published 7 years ago

koa-router-factory v1.2.0

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

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

KeyTypeDescription
urlStringURL for the route
controllerObjectObject contains all actions needed. Is optional for child route. If not defined for child route, parent controller is used
actionStringIf not specified, index action is used
childsObjectChild routes definitions
methodStringget, 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.

1.2.0

7 years ago

1.1.0

7 years ago

1.0.1

8 years ago

1.0.0

8 years ago