Socket
Book a DemoInstallSign in
Socket

koa-mvcrouter

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-mvcrouter

koa2 mvc 路由

latest
Source
npmnpm
Version
2.0.1
Version published
Maintainers
1
Created
Source

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;

其他资料

  • 文档 https://bcklib.js.org
  • QQ群:13924029

Keywords

koa

FAQs

Package last updated on 12 Aug 2019

Did you know?

Socket

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.

Install

Related posts