# @robotmayo/krouter

> A Koa Router based heavily on Express router

Latest version **0.6.0** (published 2019-02-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install @robotmayo/krouter
pnpm add @robotmayo/krouter
yarn add @robotmayo/krouter
bun add @robotmayo/krouter
```

## Health

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

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

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.0 |
| Published | 2019-02-15 |
| First published | 2018-10-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 13.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | robotmayo |
| Maintainers | robotmayo |
| Keywords | router, koa, express |

## Links

- npm: https://www.npmjs.com/package/@robotmayo/krouter
- npm.io page: https://npm.io/package/@robotmayo/krouter

## Dependencies (5)

- [@types/koa](https://npm.io/package/@types/koa.md) 2.0.46
- [koa-compose](https://npm.io/package/koa-compose.md) 4.1.0
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) 2.4.0
- [@types/koa-compose](https://npm.io/package/@types/koa-compose.md) 3.2.2
- [@robotmayo/stderror](https://npm.io/package/@robotmayo/stderror.md) ^1.0.0

## 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.6.0 (latest) — 2019-02-15
- 0.5.1 — 2019-02-15
- 0.5.0 — 2019-02-15
- 0.4.0 — 2018-10-27
- 0.3.0 — 2018-10-23
- 0.2.1 — 2018-10-17
- 0.2.0 — 2018-10-17
- 0.1.2 — 2018-10-13
- 0.1.1 — 2018-10-13
- 0.1.0 — 2018-10-13

## README

# KRouter

Simple express like router for koajs. Lets get it ready for production use.

## Install

`npm i @robotmayo/krouter`

## Features

- Express inspired routing
- Nested/"child" routers
- Async/Await ready

## Quick Start

```javascript
import KRouter from "@robotmayo/krouter";
import * as Koa from "koa";
const app = new Koa();
const router = new KRouter();

router
.use((ctx, next) => {
  // do something on every route
  return next();
})
.get("/", ctx => {
  ctx.body = "Home Page";
})
.post("/new", (ctx, next) => {
  if(ctx.isAuthorized) return next()
  ctx.status = 401
},
ctx => {
  const data = await someAsyncProcess()
  ctx.body = data
});
app.use(router.middleware());
```

## Api

`new KRouter([opts])`

Create a router instance

| Parameter     | Required | Type   | Description                                                      |
| ------------- | -------- | ------ | ---------------------------------------------------------------- |
| [opts]        | False    | object |                                                                  |
| [opts.prefix] | False    | string | Prefix to prepend to all routes declared in this router instance |

```javascript
import KRouter from "@robotmayo/krouter";
import * as Koa from "koa";
const app = new Koa();
const router = new KRouter();
router.get("/bear", (ctx, next) => {
  // middleware called on /bear
});
// or with a prefix
const prefixedRouter = new Router({ prefix: "/api" });
prefixedRouter.get("/posts", (ctx, next) => {
  // middleware called on /api/posts
});
app.use(router.middleware()).use(prefixedRouter.middleware());
```

`router.[verb](path|...middleware)`

Where verb is `use|get|put|post|del|delete|patch|options`

Mount middleware with with the specific method and optionally a path. The only exception is `use` will run on any method.

| Parameter    | Required | Type                               | Description                      |
| ------------ | -------- | ---------------------------------- | -------------------------------- |
| [path]       | False    | string                             |                                  |
| [middleware] | False    | koa middleware \| koa middleware[] | Upon match, run these middleware |

```javascript
import KRouter from "@robotmayo/krouter";
import * as Koa from "koa";
const app = new Koa();
const router = new KRouter();

router
  .use((ctx, next) => {
    // do something will always be called
    next();
  })
  .get("/tape", (ctx, next) => {
    // do stuff. only called on GET /tape
    ctx.body = 100;
  });
```

`router.mount(childRouter)`

Mount a router inside another one. Similiar to nested/child router.

| Parameter   | Required | Type    | Description                              |
| ----------- | -------- | ------- | ---------------------------------------- |
| childRouter | True     | KRouter | Mount the child inside the parent router |

When mounting routers the mounted router will take on the prefix of the parent router.

Example

```javascript
import KRouter from "@robotmayo/krouter";
import * as Koa from "koa";
const app = new Koa();
const router = new KRouter({ prefix: "/api" });
const child = new KRouter({ prefix: "/posts" });

child.get("/:id", (ctx, next) => {
  // do stuff
});
router.mount(child);
// now the child will respond to /api/posts/:id
app.use(router.middleware());
```

## Developing

The code is written in typescript so you will need to know it. Install the dependencies with `npm i` then you can transpile with `npm build`. `npm build:watch` to auto build on changes to speed up development.

## Test

Simply run `npm run test`.

## Why?

I only recently started using koa and naturually wanted a router for it. I originally used/expirmented with koa-router but this (https://github.com/ZijianHe/koa-router/issues/462) is a big problem for me. I prefer my routing to be as determinstic as possible. Most importantly I simply thought it would be fun to write a simple router. 😃

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