1.0.10 • Published 9 years ago

koa-restful v1.0.10

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

Build restfull-api server with koa

Install

koa-restful is available using npm:

npm install koa-restful

example

use : create koa app.js file ,require koa-restful and write this

目录结构

|-core
|-model
|---schema
|-public
|---image
|---lib
|---layout
|-views    
|-app.js

代码

var app = require('koa')(),
	restful = require('koa-restful');
var opt = { 
    modelPath : '../../app/models',
    controllerPath : '../../app/controllers',
    config : {
    	db : ''
    }
};
if( opt.modelPath ){
    restful(opt.modelPath, opt.controllerPath, opt.config.db, app);
}
else{
    log('app-init', 'no model folder, did not init models and models-routes');
}
app.listen(3000);
  • modelPath : model文件夹路径
  • controllerPath : controllerPath 文件夹路径
  • config : 数据库等配置信息

model 数据模型

通过实例化/prototype扩展baseModel生成一个新的model:

schemaObj很重要, 你可以在里面定义model的所有属性, 各个属性的类型和是否必须. 以user为例

actionurl含义model方法名
get/api/user/:id获取某个model._read
get/api/user?foo=bar通过query查询,获取一些(query为空时获取所有)model._readByQuery
post/api/user新建一个用户model._create
put/api/user/:id修改一个用户, updates放在request body中model._update
put/api/user?foo=bar通过query查询,修改一个用户model._update
delete/api/user/:id删除某个用户model._delete
delete/api/user?foo=bar通过query查询, 删除某些用户(query为空时会删除所有)model._deleteByQuery

:id是在schema中制定的关键属性(如自定义的id或者email), 如果没有指定的话会使用默认的_id 上述方法会在每一个controller中实现, 可以通过$$controllers_o这个全局变量访问$$controllers_o.controllerName.method(param, [field]), 路由也会自动绑定. 如果对于现有的路由不满意, 可以在schema中配置说明不添加某些路由disable默认为false即添加.然后自己去添加路由, 也可覆盖默认的model方法.

MIT Licensed

1.0.10

9 years ago

1.0.9

9 years ago

1.0.8

9 years ago

1.0.7

9 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

0.0.1

9 years ago