0.1.5 • Published 6 years ago

koa-interceptors v0.1.5

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

koa-interceptors

An interceptors middleware for koa2

NPM Version Download Status Dependencies

Introduction:

The interceptor automatically associates the router with the controller.

Usage

app.js

'use strict';

// modules
const koa = require('koa');
const convert = require('koa-convert');
const session = require('koa-session');
const Interceptors = require('koa-interceptors');

// app
const app = new koa();
// interceptors
const interceptors = new Interceptors();

// use session
interceptors.use(convert(session()));

// load routes
app.use(interceptors.routes());

/routers/home/login.js

'use strict';

module.exports = {
  '/login': [
    {
      action: 'login' // page action
    },
    {
      method: 'post', // if get can be omitted
      action: 'validate-login' // page action
    }
  ]
};

/controllers/home/login.js

'use strict';

module.exports = {
  login: ctx=>{
    // page action logic
  },
  'validate-login': ctx=>{
    // page action logic
  },
};

Install

$ npm install koa-interceptors --save

API

Interceptors(routers, controllers, options)
  • routers: the routers base dir.
  • controllers: the controllers base dir.
  • options: the koa-router options.
Others see: koa-router

Notice

The interceptor will add a property routeData on koa ctx.

0.1.5

6 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.1

8 years ago