0.6.0 • Published 5 years ago

@robotmayo/krouter v0.6.0

Weekly downloads
2
License
MIT
Repository
-
Last release
5 years ago

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

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

ParameterRequiredTypeDescription
optsFalseobject
opts.prefixFalsestringPrefix to prepend to all routes declared in this router instance
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.

ParameterRequiredTypeDescription
pathFalsestring
middlewareFalsekoa middleware | koa middleware[]Upon match, run these middleware
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.

ParameterRequiredTypeDescription
childRouterTrueKRouterMount the child inside the parent router

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

Example

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. 😃

0.6.0

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago