# koa-qc-router

> Router middleware for koa.

Latest version **1.1.1** (published 2019-11-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-qc-router
pnpm add koa-qc-router
yarn add koa-qc-router
bun add koa-qc-router
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2019-11-14 |
| First published | 2019-11-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | QC |
| Maintainers | qczxl |

## Links

- npm: https://www.npmjs.com/package/koa-qc-router
- npm.io page: https://npm.io/package/koa-qc-router

## Recent versions

- 1.1.1 (latest) — 2019-11-14
- 1.1.0 — 2019-11-14
- 1.0.8 — 2019-11-14
- 1.0.7 — 2019-11-14
- 1.0.6 — 2019-11-14
- 1.0.5 — 2019-11-14
- 1.0.4 — 2019-11-13
- 1.0.3 — 2019-11-13
- 1.0.2 — 2019-11-13
- 1.0.1 — 2019-11-13
- 1.0.0 — 2019-11-13

## README

## koa-qc-router

Router middleware for koa

## Install

```js
npm install --save koa-qc-router
```

## Usage
```js
const Koa = require('koa') // koa v2.x
const path = require('path')
const router = require('koa-qc-router')

var app = new Koa()

app.use(router(
    // ①
    { 
        'Index' : [
            ['Index', require(`./app/Index/Index`)]
        ]
    }
))

app.use(async (ctx, next) => {
    //404
    console.log(`${ctx.group}/${ctx.control}/${ctx.action} is not find`)
})

/*
    Explained ①：
    Group name and controller name capitalization.
    {
        'Group Name' : [
            ['Controller name', Controller file module ]
        ]
    }

 */

```


```js
const Koa = require('koa') // koa v2.x
const path = require('path')
const router = require('koa-qc-router')

var app = new Koa()
app.use(router(
    { 
        'Index' : [
            ['Index', require(`./app/Index/Index`)]
        ]
    },
    // ②
    [  
        ['news' , 'index/index/news'],   
        ['news/info' , 'index/index/info']
    ]
))

app.use(async (ctx, next) => {
    //404
    console.log(`${ctx.group}/${ctx.control}/${ctx.action} is not find`)
})

/*
    Explained ②：
    Route mapping relationship, the controller module to be mapped
    [   
        ['news' , 'index/news/index']
    ]

 */

```

## Rule

Take `index/index/info` as an example, create it under the project path.

1, create a grouping unit folder `Index`, pay attention to the first letter capitalization

2, create a controller file `Index.js` under the group, the first letter is capitalized

3, add the `info` method in the controller file

```js
// Index.js

class Index {
    
    constructor(ctx) {
        this.ctx = ctx
    }

    async info() {
        //...
    }

}

module.exports = Index

```

## Default router
```js
// http://127.0.0.1:3000  => group => Index   control => Index   action => index

// When no mapping is set
// http://127.0.0.1:3000/user  => group => User   control => Index   action => index
// http://127.0.0.1:3000/user/info  => group => User   control => Info   action => index
// http://127.0.0.1:3000/user/info/upload  => group => User   control => Info   action => upload

//When there is a mapping relationship
[   
    ['news' , 'index/news/info']
]
// http://127.0.0.1:3000/news  => group => Index   control => News   action => info
```

## Get parameter
```js
// When no mapping is set
// http://127.0.0.1:3000/user/info/upload/id/1/type/1  =>  ctx.query.id  ctx.query.type

//When there is a mapping relationship
[   
    ['news', 'index/news/index']
]
// http://127.0.0.1:3000/news/id/1  => ctx.query.id
```

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