# @koex/router

> router for koa extend

Latest version **1.0.2** (published 2022-04-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install @koex/router
pnpm add @koex/router
yarn add @koex/router
bun add @koex/router
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2022-04-25 |
| First published | 2018-12-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 11.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Zero |
| Maintainers | uniquecolesmith |

## Links

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

## Dependencies (4)

- [@koex/type](https://npm.io/package/@koex/type.md) ^1.0.2
- [@zcorky/lru](https://npm.io/package/@zcorky/lru.md) ^1.0.5
- [koa-compose](https://npm.io/package/koa-compose.md) ^4.1.0
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^3.1.0

## Recent versions

- 1.0.2 (latest) — 2022-04-25
- 1.0.1 — 2022-04-25
- 0.13.36 — 2021-10-10
- 0.13.34 — 2021-06-14
- 0.13.29 — 2021-05-13
- 0.13.28 — 2021-05-13
- 0.13.24 — 2021-05-13
- 0.13.23 — 2021-05-13
- 0.13.20 — 2021-05-07
- 0.13.18 — 2021-05-06
- 0.13.17 — 2021-05-06
- 0.13.13 — 2021-05-02
- 0.13.11 — 2021-05-01
- 0.13.6 — 2021-05-01
- 0.13.5 — 2021-05-01
- … 49 more at https://npm.io/package/@koex/router/versions

## README

# router

[![NPM version](https://img.shields.io/npm/v/@koex/router.svg?style=flat)](https://www.npmjs.com/package/@koex/router)
[![Coverage Status](https://img.shields.io/coveralls/koexjs/router.svg?style=flat)](https://coveralls.io/r/koexjs/router)
[![Dependencies](https://img.shields.io/david/koexjs/router.svg)](https://github.com/koexjs/router)
[![Build Status](https://travis-ci.com/koexjs/router.svg?branch=master)](https://travis-ci.com/koexjs/router)
![license](https://img.shields.io/github/license/koexjs/router.svg)
[![issues](https://img.shields.io/github/issues/koexjs/router.svg)](https://github.com/koexjs/router/issues)
> Simple Router for Koa

### Install

```
$ npm install @koex/router
```

### Usage

```javascript
// See more in test
import * as router from '@koex/router';

import * as Koa from 'koa';
const app = new Koa();

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

app.use(router.get('/health', async (ctx) => {
  ctx.status = 200;
  ctx.body = 'ok';
}));

app.use(router.get('/product/:pid', async (ctx) => {
  ctx.body = ctx.params.pid;
}));

// support middlewares for router
const md5 = crypto.createHash('md5').update('123').digest('hex');

const responseTime = async (ctx, next) => {
  const start = Date.now();
  await next();
  ctx.set('X-Response-Time', Date.now() - start);
};

const requestId = async (ctx, next) => {
  await next();
  const id = md5(ctx.url + Date.now());
  ctx.set('X-Request-Id', id);
};

const handler = async (ctx) => {
  ctx.body = ctx.params.pid + ': ' + ctx.params.cid;
};

app.use(router.get('/product/:pid/:cid', responseTime, requestId, handler));

// fallback
app.use(async (ctx) => {
  ctx.body = {
    name: 'name',
    value: 'value',
  };
});

app.listen(8000, '0.0.0.0', () => {
  console.log('koa server start at port: 8000');
});
```

### Related
* [koa-router](https://github.com/alexmingoia/koa-router)
* [koa-route](https://github.com/koajs/route)
* [koa-compose](https://github.com/koajs/compose)

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