0.5.0 • Published 7 years ago

koa-trout v0.5.0

Weekly downloads
12
License
MIT
Repository
github
Last release
7 years ago

koa-trout

Trie router for Koa

Based on toa-router

Setup

NPM:

npm install --save koa-trout

Yarn:

yarn add koa-trout

Usage

Basic

const Koa    = require('koa');
const Router = require('koa-trout');

const koa    = new Koa;
const router = new Router;

router.get('/hello',
    async function (context) {
        context.body = 'Hello, world!';
    }
);

koa.use(router.routes);

koa.listen(8080);

Params

const Koa    = require('koa');
const Router = require('koa-trout');

const koa    = new Koa;
const router = new Router;

router.get('/hello/:to',
    async function (context, next) {
        context.body = `Hello, ${ context.params.to }!`;

        await next();
    }
);

koa.use(router.routes);

koa.listen(8080);

Middleware

const Koa    = require('koa');
const Router = require('koa-trout');

const koa    = new Koa;
const router = new Router;

router.use(async function (context, next) {
    if (!context.get('authorization') !== 'password') {
        context.throw(401, 'unauthorized');
    }

    await next();
});

router.get('/secret',
    async function (context, next) {
        context.body = "You're in!";

        await next();
    }
);

koa.use(router.routes);

koa.listen(8080);

Multiple handlers

Handlers are just koa middleware, so you can mount route-specific middleware, and any number of handlers for a route.

const Koa    = require('koa');
const Router = require('koa-trout');
const cors   = require('kcors');
const body   = require('koa-body');

const koa    = new Koa;
const router = new Router;

router.post('/users',
    cors(),
    body(),
    async function (context, next) {
        context.state.user = await User.query()
            .insert(context.request.body);

        await next();
    },
    async function (context, next) {
        context.body = context.state.user.toJSON();

        await next();
    }
);

koa.use(router.routes);

koa.listen(8080);

Multiple methods

const Koa    = require('koa');
const Router = require('koa-trout');
const body   = require('koa-body');

const koa    = new Koa;
const router = new Router;

router.route(['patch', 'put'], '/users/:id',
    body(),
    async function (context, next) {
        context.state.user = await User.query()
            .patchAndFetchById(context.params.id, context.request.body);

        await next();
    },
    async function (context, next) {
        context.body = context.state.user.toJSON();

        await next();
    }
);

koa.use(router.routes);

koa.listen(8080);

Nested routers

const Koa    = require('koa');
const Router = require('koa-trout');
const body   = require('koa-body');

const koa    = new Koa;
const router = new Router;
const api    = new Router;

router.get('/', async function (context, next) {
    context.body = 'Home';

    await next();
});

api.get('/users', async function (context, next) {
    context.body = { name: 'Bob' };

    await next();
});

router.mount('/api', api);

koa.use(router.routes);

koa.listen(8080);
0.5.0

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago