# @lvchengbin/koa-router

> Koa router middleware

Latest version **0.1.1** (published 2019-01-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install @lvchengbin/koa-router
pnpm add @lvchengbin/koa-router
yarn add @lvchengbin/koa-router
bun add @lvchengbin/koa-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.1.1 |
| Published | 2019-01-09 |
| First published | 2018-01-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.0 |
| Dependencies | 2 |
| Unpacked size | 9.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 4 |
| Author | LvChengbin |
| Maintainers | lvchengbin |
| Keywords | koa, middleware, router, route |

## Links

- npm: https://www.npmjs.com/package/@lvchengbin/koa-router
- Repository: https://github.com/LvChengbin/koa-router
- Homepage: https://github.com/LvChengbin/koa-router#readme
- Issues: https://github.com/LvChengbin/koa-router/issues
- npm.io page: https://npm.io/package/@lvchengbin/koa-router

## Dependencies (2)

- [@lvchengbin/is](https://npm.io/package/@lvchengbin/is.md) 0.0.24
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^2.1.1

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 0.1.1 (latest) — 2019-01-09
- 0.1.0 — 2019-01-07
- 0.0.10 — 2019-01-05
- 0.0.9 — 2018-05-17
- 0.0.8 — 2018-05-12
- 0.0.7 — 2018-05-12
- 0.0.6 — 2018-04-24
- 0.0.5 — 2018-02-05
- 0.0.4 — 2018-02-05
- 0.0.3 — 2018-02-05
- 0.0.2 — 2018-01-17
- 0.0.1 — 2018-01-16

## README

# koa-router

A simple routing middleware for koa which can match path string, and queries.

## Installation

```js
$ npm i @lvchengbin/koa-router --save
```
## Usage

A simple example:

```js
const Koa = require( 'koa' );
const Router = require( '@lvchengbin/koa-router' );

const app = new Koa();
const router = new Router( app );

router.get( '/user', async ctx => {
    ctx.body = 'User';
} );

router.get( '/user/:id', async ( ctx, next, id ) => {
    ctx.body = 'User: ' + id;
} );

router.post( '/add', async ctx => {
    ctx.body = 'Add data';
} );

app.listen( 3000 );
```

You can also use `RegExp` for the router:

```js
const Koa = require( 'koa' );
const Router = require( '@lvchengbin/koa-router' );

const app = new Koa();
const router = new Router( app );

router.get( /^\/user\/(\d+)/, async ( ctx, next, id ) => {
    ctx.body = id;
} );

app.listen( 3000 );
```

You can also try matching queries in query string:

```js
const Koa = require( 'koa' );
const Router = require( '@lvchengbin/koa-router' );

const app = new Koa();
const router = new Router( app );

// match all requests with a query responseType and it's value should be jsonp
router.get( { responseType : 'jsonp' } , async ( ctx, next ) => {
    ctx.body = id;
} );

// match all id start with 1
router.get( { id : /^1(\d+)/ }, async ctx => {
} );

app.listen( 3000 );
```

If you want to add a route for multiple types of methods, you can use `router.any` method, for example:

```js
const app = new Koa();
const router = new Router( app );

router.any( [ 'get', 'post' ], '/api', async ctx => {
    ctx.body = 'Method: ' + ctx.method;
} );
```

Using asterisk (*) as the first argument of `router.any` would match all methods:

```js
router.any( '*', '/api', async ctx => {
    ctx.body = 'This would be execute with all methods';
} );
```

Multiple `path`s can be set as an `Array` or a `Generator` in one rule:

```js
// using an array
router.get( [ '/path/a', '/path/b' ], async ( ctx, next ) => {
} );

// using a generator
router.get( function*() {
    yield '/path/a';
    yield '/path/b';
}, async ( ctx, next ) => {
} );
```

In default situation, if you pass the instance of `Koa` to the constructor of `koa-router`, `app.use` would be called automatically. Therefore, if you don't want to execute `app.use` automatically, you don't need to pass the `Koa` instance to the constructor of `koa-router`. Then, you can also call `app.use` later.

```js
const app = new Koa();
const router = new Router();

app.use( router.get( '/user', async ctx => {
    ctx.body = 'User';
} ) );
```

To execute multiple middlewares with a routing rule.

```js
const app = new Koa();
const router = new Router( app );

router.get( '/', [
    ( ctx, next ) => {
        next();
    },
    ctx => {
        ctx.body = 'Hello world!';
    }
] );
```

Route paths would be parsed by [path-to-regexp](https://github.com/pillarjs/path-to-regexp);

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