1.3.0 • Published 6 years ago

lark-router-config v1.3.0

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

lark-router-config

Config lark routers in an easy way

NPM version build status Test coverage NPM downloads Node.js dependencies

install

$ npm install lark-router-config

config router with a config file (.yaml or .json)

const Koa           = require('koa');
const Router        = require('koa-router');
const RouterConfig  = requrie('lark-router-config');

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

const routes = new RouterConfig('routes.yaml');
routes.inject(router, { directory: 'routes' });

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

Config file: ./routes.yaml

welcome:    GET   /yaml/welcome
home:
  user:     GET   /yaml/home/:name
  profile:  GET   /yaml/home/:name/profile
article:
  read:     GET   /yaml/article/:title
  comment:  POST  /yaml/article/:title/comment

Directory ./routes:

welcome.js
home/
  user.js
  profile.js
article/
  read.js
  comment.js

LarkRouter will load all files and add them into route rules.

config router with a directory directly

const Koa           = require('koa');
const Router        = require('koa-router');
const RouterConfig  = requrie('lark-router-config');

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

const routes = new RouterConfig('controllers');
routes.inject(router, { param: '.as.param', asterisk: '.as.asterisk' });

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

Directory ./controllers:

welcome.js                => /welcome
home/
  user.as.param.js        => /home/:user
  name.as.param/
    profile.js            => /home/:name/profile
article/
  read.as.asterisk.js     => /article/:read*
  title.as.asterisk/
    comment.js            => /article/:title*/comment

in welcome.js

exports.GET = async (ctx, next) => {...}
exports.POST = async (ctx, next) => {...}
1.3.0

6 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago