# chestnut-router

> for chestnut-app

Latest version **0.2.4** (published 2018-05-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install chestnut-router
pnpm add chestnut-router
yarn add chestnut-router
bun add chestnut-router
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.4 |
| Published | 2018-05-09 |
| First published | 2017-08-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 12.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | nandy007 |
| Maintainers | nandy007 |

## Links

- npm: https://www.npmjs.com/package/chestnut-router
- Repository: https://github.com/nandy007/chestnut-router
- Homepage: https://github.com/nandy007/chestnut-router#readme
- Issues: https://github.com/nandy007/chestnut-router/issues
- npm.io page: https://npm.io/package/chestnut-router

## Dependencies (2)

- [glob](https://npm.io/package/glob.md) ^7.1.2
- [koa-router](https://npm.io/package/koa-router.md) ^7.2.1

## Recent versions

- 0.2.4 (latest) — 2018-05-09
- 0.2.3 — 2018-05-08
- 0.2.2 — 2018-04-02
- 0.2.1 — 2018-03-21
- 0.2.0 — 2017-08-12
- 0.1.0 — 2017-08-04
- 0.0.2 — 2017-08-04
- 0.0.1 — 2017-08-03

## README

# chestnut-router Koa2快速路由

配合[chestnut-app](https://github.com/nandy007/chestnut-app)使用，也可单独在Koa2中使用

## 用法

app.js

```javascript

const Koa = require('koa');

const path = require('path');

const router = require('chestnut-router');

const app = new Koa();

// 应用的根目录
const rootPath = path.join(__dirname);

app.use(router(rootPath));

```

这时候需要在router代码中使用router.create方法来使用路由。


## 静态方法

### create 创建路由

create方法接受两个参数，第一个是父目录，第二个是过滤器数组

routers目录下的路由定义：
```javascript

// 返回的是koa-router对象，代表父目录为/interface，如果是根目录使用/或者空串
const router = require('chestnut-router').create('/interface');
// 使用过滤器写法
// const router = require('chestnut-router').create('/interface', ['session']);

module.exports = router
  .get('/login', async function(ctx){
		ctx.body = 'hi chestbut app';
	}); // 访问路径为http://ip:port/interface/login

```

### addFilters 添加过滤器

addFilters接受一个参数，为过滤规则对象，形如：{key : {handler: function(ctx){}}} 或者 {key: function(ctx){}}

当函数返回false则所有中间件全部处理结束，如果返回null则所有过滤器处理结束，返回其他值或者不返回则后续过滤器和中间件继续处理

比如：
```javascript

const router = require('chestnut-router');

router.addFilters({
	session : {
        handler : function(ctx){
            const hasData = ctx.session.hasData;
            if(!hasData) ctx.body = {
                result : 'fail',
                msg : '会话超时'
            };
            return hasData;// 没有会话时返回false，后续过滤器和中间件不处理
        }
    },
	fliter1 : function(ctx){
		console.log('进入了过滤器');
	}
	
});

```

过滤器可以在create中使用，也可以在excuteFiters中使用

使用的时候过滤器数组元素必须为过滤器的key

### excuteFiters 执行过滤器

excuteFiters接受一个参数，此次参数为过滤器数组，数组的元素必须是addFilters方法添加的规则的key或者直接是一个function(ctx){}函数，此函数规则跟addFilters中要求的函数有一致的要求


在koa中间件中使用

```javascript

const router = require('chestnut-router');

app.use(router.excuteFiters(['session', function(ctx){
	console.log('经过了临时过滤器');
}]));

```

在路由创建时使用
```javascript

const router = require('chestnut-router').create('/interface', ['session']);

```

在路由定义时使用
```javascript

// 返回的是koa-router对象，代表父目录为/interface，如果是根目录使用/
const router = require('chestnut-router').create('/interface');

module.exports = router
  .get('/login', router.excuteFiters(['session'], async function(ctx){
		ctx.body = 'hi chestbut app';
	}); // 访问路径为http://ip:port/interface/login

```


使用自定义过滤器

```javascript

const router = require('chestnut-router');

const custom = function(ctx){
	// do sth.
};

app.use(router.excuteFiters(['session', custom]));

```

### 链式路由

有的时候需要将一组url进行统一控制使用过滤器，可以使用链式路由。

即使用router的add函数来添加子路由。

add函数支持任意多个参数，每个参数必须符合下列条件之一：

1.入参router对象

2.入参字符串，其代表某个存在的本地目录路径，该路径下的所有js文件对应的模块必须是router对象并全部作为子路由添加进来。


比如：

```javascript

const routerMiddleware = require('chestnut-router');

// 创建主路由
const router = routerMiddleware.create('/interface');
// 创建子路由
const childRouter = routerMiddleware.create('/');

// 要求childRouterPath下的所有js文件作为模块引入时为router对象
const childRouterPath = path.join(__dirname, '../../childs');

// 子路由创建请求
childRouter
    .get('child', async function(ctx, next){
        ctx.body = '使用了子路由的响应';
    });

module.exports = router.add(childRouter, childRouterPath);


```

---
_Source: https://npm.io/package/chestnut-router · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
