https://github.com/nuintun/koa-interceptors
An Interceptors middleware for koa2
https://github.com/nuintun/koa-interceptors
Last synced: about 1 year ago
JSON representation
An Interceptors middleware for koa2
- Host: GitHub
- URL: https://github.com/nuintun/koa-interceptors
- Owner: nuintun
- License: mit
- Created: 2016-09-27T02:47:59.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-04-12T09:18:32.000Z (about 8 years ago)
- Last Synced: 2024-08-31T08:38:09.591Z (almost 2 years ago)
- Language: JavaScript
- Homepage:
- Size: 25.4 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# koa-interceptors
>An interceptors middleware for koa2
>
>[![NPM Version][npm-image]][npm-url]
>[![Download Status][download-image]][npm-url]
>[![Dependencies][david-image]][david-url]
### Introduction:
The interceptor automatically associates the router with the controller.
### Usage
`app.js`
```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`
```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`
```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][koa-router-url] options.
##### Others see: [koa-router][koa-router-url]
### Notice
The interceptor will add a property `routeData` on koa ctx.
[npm-image]: http://img.shields.io/npm/v/koa-interceptors.svg?style=flat-square
[npm-url]: https://www.npmjs.org/package/koa-interceptors
[download-image]: http://img.shields.io/npm/dm/koa-interceptors.svg?style=flat-square
[david-image]: http://img.shields.io/david/nuintun/koa-interceptors.svg?style=flat-square
[david-url]: https://david-dm.org/nuintun/koa-interceptors
[koa-router-url]: https://github.com/alexmingoia/koa-router