1.0.5 • Published 7 years ago

koa-router-plus v1.0.5

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

koa-router-plus

Create controller and attach related router

Thanks To

Idea comes from koa-router and koa-simple-controller

Installation

Install using npm:

npm install koa-router-plus --save

Usage

index.js

'use strict';

var app = require('koa')();
var router = require('koa-router-plus')();

app.use(router.initialCtrl());
app.use(router.routes());
app.listen(3000);

lib/controller/index.js

module.exports = {
    index : function(ctx) {
        ctx.body='Welcome to drink bar, What kind of drink do you like?'
    },
    
}

lib/controller/help/coffee.js

module.exports = {
    index : function(ctx) {
        ctx.body='Hello, What kind of coffee do you like?'
    },
    
    moreSugar: function(ctx) {
        ctx.body='Sugar, please!'
    },

    moreMilk: function(ctx) {
        ctx.body='Milk, please!'
    }
}

lib/controller/help/Tea.js

module.exports = {
    index : function(ctx) {
        ctx.body='Hello, What kind of tea do you like?'
    },
    
    hotter: function(ctx) {
        ctx.body='Hey, heat it please!'
    },
}

Browser

URL: http://localhost:3000/, Means call index.index().

URL: http://localhost:3000/help/coffee, Means call help.coffee.index().

URL: http://localhost:3000/help/coffee/moreSugar, Means call help.coffee.moreSugar().

Contribute

You are welcome to contribute.

License

MIT

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago