• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

xpepermint/koa-controller: MVC-style implementation of routes and controllers fo ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

xpepermint/koa-controller

开源软件地址:

https://github.com/xpepermint/koa-controller

开源编程语言:

JavaScript 100.0%

开源软件介绍:

koa-controller

Build Status NPM version Dependency Status

Koa-controller in a middleware for Koa which handles the routing of your application where related functionalities are splitted into routes, controllers and constraints. The module is built on top of koa-route middleware. It optimizes your code and brings the following features into your project:

  • Flexible routes handler with a single point of router configuration.
  • Application controllers for handling application responses.
  • Access control middleware with constraints for limiting requests to application controllers, handling user authentication and security.
  • Context tools for easy dynamic data manipulation.

Installation

Install the npm package.

npm install koa-controller --save

Attach the middleware.

var koa = require('koa');
var app = koa();
var kc = require('koa-controller');
app.use(kc.tools()); // optional
app.use(kc.router());
app.listen(3000);

By default the middleware expects that controllers exist at app/controllers/{controller}.js, constraints at app/constraints/{constraint}.js and the router configuration file at config/routes.js. We can easily change the default behavior as shown bellow.

app.use(controller({
  routesPath: 'my/path/routes.js',
  controllerPath: 'my/controllers/{controller}.js', // note that {controller} is a variable
  constraintPath: 'my/constraints/{constraint}.js', // note that {constraint} is a variable
  logger: console.log // custom logger function
}));

Note that routesPath and controllerPath must exist where constraintPath is not required.

Routes

Routes file is a simple key-value object where the key represents a route and the value represents a task. Create a new file and define your project's routes based on the example bellow.

// config/routes.js
module.exports = {

  // controller#action
  '/users/:id?': { to: 'users#find' },
  'post /users': { to: 'users#create' },
  'put|post /users/:id': { to: 'users#update' },
  'get /users/:id/words/:slug*': { to: 'events#words' },
  'get /event/:slug+': { to: 'events#index', constraint: 'api#ip' },

  // redirections
  'get /to/google': { to: 'http://www.google.com' },
  'get /to/home': { to: '/' },

  // using a function
  'get /events/:id': { to: function *(id) { this.body = ... } },

  ...
};

You check koa-route and path-to-regexp for more information.

Controller

Controller is a simple key-value object where the key represents the name of an action and the value represents a generator function that processes the request. Create a new file for your first controller and define actions based on the example bellow. Don't forget to connect the new controller with a route inside routes.js file.

// app/controllers/users.js
module.exports = {

  find: function*() {
    this.body = ...;
  },

  update: function*(id) {
  },

  words: function*(id, slug) {
  },

  ...
};

Notice the this.body call? Every action inside a controller has access to Koa context. Check koa-route for details.

Constraint

Constraint is a simple key-value object where the key represents the name of a constraint and the value represents a generator function that processes the request. Create a new file for your first constraint and define constraints based on the example bellow. Don't forget to connect the new constraint with a route inside routes.js file.

// app/constraints/api.js
module.exports = {

  ip: function*(next) {
    if (this.request.ip == '192.168.1.100') { // allow access only from this IP address
      yield next;
    } else {
      this.body = 'Unauthorized IP address';
      this.status = 401;
    }
  },

  ...
};

Note that constraints are very much like controllers thus every constraint action has access to Koa context. Check koa-route for details.

Tools

By attaching kc.tools() middleware the context features are extended.

ctx.form([names])

Type: Function Returns: Object

Parsed request body data. You can retrive only selected attributes by specifying a list of names.

console.log( _.form() );
// -> { 'name': 'John', 'email': '[email protected]', 'age': 33 }
console.log( _.form('name', 'age') );
// -> { 'name': 'John', 'age': 33 }



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
cultofmetatron/koa-todo: a basic todo app in koa发布时间:2022-07-10
下一篇:
TIME-GATE/koa-ctp-service: 用koa、c++写的量化交易系统发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap