2.0.1 • Published 5 years ago

koa-mvcrouter v2.0.1

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

koa-mvcrouter

koa2 mvc 路由

安装到工程

npm i koa-mvcrouter -S

引用

const mvcrouter = require('koa-mvcrouter');

加载路由

app.js中

app.use(mvcrouter.load());

工程结构

controllers存放路由(控制器),views 存放视图,views/_layout 存放布局页 自动匹配views目录下于controller同名目录下action同名的视图 自动匹配_layout目录下default.ejs

project
    node_modules
    controllers
        test.js
    views
        _layout
            default.ejs
            default.h5.ejs
        test.ejs

控制器(路由)中 action 示例

const mvcrouter = require('../modules/koa-mvcrouter');

mvcrouter.viewGET('/', function (ctx) {
  return {
    title: '这是一个GET视图响应'
  };
});

mvcrouter.viewGET('/uv', function (ctx) {
  return {
    title: '这这里自定义test布局'
  };
}, null, '', 'test');

mvcrouter.viewGET('/rest/:id/:name', function (ctx) {
  return {
    title: 'RESTful GET action',
    content: '自定义视图和布局页'
  };
}, null, 'users/index', 'test');

mvcrouter.jsonPOST('/json', function (ctx) {
  return {
    title: 'json test',
    reqData: ctx.request.body
  };
});

mvcrouter.jsonPUT('/rest', async function (ctx) {
  return {
    res: 'RESTful PUT action',
    reqData: ctx.request.body
  };
});

mvcrouter.jsonDELETE('/rest/:id', async function (ctx) {
  return {
    res: 'RESTful DELETE action',
    id: ctx.params.id,
    reqData: ctx.request.body
  };
});

mvcrouter.textGET('/text', function (ctx) {
  return 'aaaaabbbb111xxx';
});

module.exports = mvcrouter;

其他资料

2.0.1

5 years ago

2.0.0

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago