koa-pattern

1.0.0 • Public • Published

koa-pattern

Build status NPM version NPM downloads Dependency Status License

A Really Simple koa middleware that support mvc and rest , including template rendering middleware.

Note: This middleware is for koa@2. koa@1 test not recovery.

Installation

$ npm install koa-pattern
$ npm run example

Templating engines

koa-pattern is using consolidate for veiw render under the hood.

List of supported engines

Example

var pattern = require('koa-pattern');
 
app.use(pattern({
        dir: __dirname,
        prefix: '',     // url prefix or virtual directory
        mvcPrefix: 'pages',//if undefine or '',null,  not run mvc  pattern
        restPrefix: 'api',//if undefine or '',null,  not run rest pattern
        //if have used others view middleware or template Engine, and has ctx.render method, can not config tmplEngine
        tmplEngine: 'dot'//or {name:'dot', 'extension':'html'}
    }));
  • opts.dir: Where your ctrls & views are located. this location must direct include ctrls/views, Must be an absolute path.
  • opts.prefix (optional) url prefix or virtual directory
  • opts.mvcPrefix: url mvc prefix
  • opts.restPrefix: url rest prefix
  • opts.tmplEngine: template Engine name string or object include name and extension: {name:'dot', 'extension':'html'}

For more examples you can take a look at the example.

Demo Dir

example
    |-ctrls
    |   |-user.js
    |-views
    |    |-user
    |        |-index.html
    |        |-list.html
    |-index.js

mvc pattern HTTP GET http://localhost/pages/ctrlName/actionName

eg: HTTP GET http://localhost/pages/user/list It will render views dir user/list.html automatic, and view's data return by ctrls/user.js list function

if not ctrlName or actionName, it default 'index', and if not index dir, root view default

//ctrls/user.js
module.exports.list = (ctx, next) => {
  return [{
      userName: 'bd',
      userNo: '007'
  },{
      userName: 'bd2',
      userNo: '9527'
  }]
}

rest pattern HTTP GET/POST/PUT/DELETE http://localhost/restPrefix/resourceName/[respration]

Rest Pattern support 2 url style to render api json data eg:

  1. HTTP GET/POST/PUT/DELETE http://localhost/api/user/007
  2. HTTP GET/POST http://localhost/api/user/list

actually, 1 and 2 is just the same rule url

and the 1 will render json by match HTTP method matched function, and 007 will be url argument, eg 007 is userid, if not HTTP method matched function, it will match ctrls/user.js 007/list methods, match rule

    'GET': 'query',
    'POST': 'update',
    'PUT': 'create',
    'DELETE': 'remove'

so url 1 return list as below, url2 if HTTP METHOD IS GET will match query return

module.exports.list = (ctx, next) => {
  return [{
      userName: 'bd',
      userNo: '007'
  },{
      userName: 'bd2',
      userNo: '9527'
  }]
}

module.exports.query = (ctx, next) => {
  return {
      userName: 'bd',
      userNo: '007'
  }
}

rest and mvc pattern async supported

module.exports.asynclist = (ctx, next) => {
    var data = {}
    var kk = () => new Promise((resolve, reject) => {
        setTimeout(function(){
            resolve([{
                userName: 'bd',
                userNo: '007'
            },{
                userName: 'bd2',
                userNo: '9527'
            }])
        },1000)
    })
    return kk().then((data) => {
        return data
    });
}

License

MIT

Package Sidebar

Install

npm i koa-pattern

Weekly Downloads

1

Version

1.0.0

License

MIT

Last publish

Collaborators

  • joecora