koa-ovenware
Version:
Automatic Model / Controller Loader for Koa
200 lines (173 loc) • 4.43 kB
Markdown
Koa-Ovenware
=======
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![NPM Monthly Downloads][npm-download]][npm-url]
[![Dependencies][david-image]][david-url]
[![License][license-image]][license-url]
[![Tips][tips-image]][tips-url]
Automatic Model / Controller Loader for Koa
* Write a controller and get all route pattern you want.
* Compatible with other middlewares including view renders.
Install
-------
```sh
npm install koa-ovenware --save
```
Simple Usage
------------
```js
var kow = require('koa-ovenware');
```
```js
kow(app);
```
Default path of controllers: ./lib/controllers/
in index.js:
```js
exports.index = function *() {
this.body = 'hello koa';
};
```
Checkout the [examples](https://github.com/zedgu/koa-ovenware/tree/master/examples).
Conventions
-----------
```
route http method function of ctrl
:resource get index
:resource post create
:resource/:id get get
:resource/:id put update
:resource/:id del del
```
All routes can be customized by setting, see [Default values](
Resource name will be the file name of the controller, if there is no alias set for the controller, see [APIs - Alias](
APIs
----
```js
kow(app[, options])
```
`options` see [Default values](#default-values)
####Controller APIs
#####Alias
Set alias for the controller.
```js
exports.alias = 'name_you_want';
```
Set routes for the controller.
```js
exports.routes = {
entry: {
method: 'get',
path: '/index'
}
};
```
Get model object.
```js
/**
* get model object by given controller file name
*
* @param {String} modelName optional, undefined for the model has
* the the same name as this controller
* @return {Object} model object
*/
this.model([modelName])
exports.model([modelName])
```
for exmample:
```js
exports.get = function *() {
this.model('abc');
};
// or
exports.todo = function() {
this.model(); // this === exports
};
```
Get controller object.
```js
/**
* get ctrl object by given controller file name
*
* @param {String} ctrlName optional, undefined for self
* @return {Object} ctrl object
*/
this.ctrl([ctrlName])
exports.ctrl([ctrlName])
```
for exmample:
```js
exports.get = function *() {
this.ctrl(); // => return this exports
};
// or
exports.todo = function() {
exports.ctrl('abc');
};
```
Global configuration
--------------------
```js
{
root: './lib', // root dir
ctrl: 'controllers', // controllers dir
model: 'models' // model dir
format: 'json', // format by default
prefix: '/', // String or RegExp
aliases: {
'index': ''
},
routes: {
'index': {
method: 'get',
path: ''
},
'create': {
method: 'post',
path: ''
},
'get': {
method: 'get',
path: '/:id'
},
'update': {
method: 'put',
path: '/:id'
},
'del': {
method: 'del',
path: '/:id'
}
}
}
```
License
-------
MIT
[]: https://img.shields.io/npm/v/koa-ovenware.svg?style=flat-square
[]: https://npmjs.org/package/koa-ovenware
[]: https://img.shields.io/travis/zedgu/koa-ovenware.svg?style=flat-square
[]: https://travis-ci.org/zedgu/koa-ovenware
[]: https://img.shields.io/coveralls/zedgu/koa-ovenware.svg?style=flat-square
[]: https://coveralls.io/r/zedgu/koa-ovenware?branch=master
[]: http://img.shields.io/david/zedgu/koa-ovenware.svg?style=flat-square
[]: https://david-dm.org/zedgu/koa-ovenware
[]: https://nodei.co/npm/koa-ovenware.png?downloads=true
[]: https://nodei.co/npm/koa-ovenware/
[]: http://img.shields.io/npm/l/koa-ovenware.svg?style=flat-square
[]: https://github.com/zedgu/koa-ovenware/blob/master/LICENSE
[]: http://img.shields.io/npm/dm/koa-ovenware.svg?style=flat-square
[]: http://img.shields.io/gittip/zedgu.svg?style=flat-square
[]: https://www.gittip.com/zedgu/