podviaznikov / koa-66

Router middleware for koa 2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

koa-66

Node.js Version NPM version build status Dependency Status

Router middleware for koa v2. Inspired by koa-router

feedbacks are welcome

Features

  • Express like http verbs methods
  • Express like use function
  • Express like param function
  • Automatic OPTIONS response
  • Automatic HEAD when GET is present
  • 501 and 405 status
  • Mount instance on specific path
  • Multiple middleware as arguments
  • Multiple middleware as array

Instalation

# npm install koa-66

Usage

const Koa = require('koa');
const Router = require('koa-66');
const app = new Koa();

const router = new Router();
const mainRouter  = new Router();

router.param('id', (ctx, next, id) => {
        ctx.yolo = id;
        return next();
});

router.use(async (ctx, next) => {
    ctx.a = " ";
    await next();
});

router.get('/:id', (ctx, next) => {
    return next().then(() => {
        ctx.body += ctx.a + ctx.yolo;
   })
});

router.get('/:id', async ctx => {
    ctx.body = await Promise.resolve('hello');
});

mainRouter.mount('/pouet', router);

app.use(mainRouter.routes());

app.listen(1664);
// GET http://localhost:1664/pouet/world
// => hello world

Test

# npm test

why?

Why not use koa-route?

  • Is a little minimalistic

Why not use koa-router?

  • For now koa-router is broken with the new koa release #186

About

Router middleware for koa 2

License:MIT License


Languages

Language:JavaScript 100.0%