@tofurama3000/tree-router

0.0.17 • Public • Published

Tree Router

Basic router based off of a tree datastructure.

Basic usage with Koa:

const Koa = require('koa')
const koaBody = require('koa-body')
const router = require('./router/koa')
const app = new Koa()

const routes = router([
  [
    '/add',
    {
      get: {
        parameters: { query: ['x', 'y'] },
        handler: ({ query: { x, y } }) => +x + +y
      },
      post: {
        handler: ({ body: { x, y } }) => +x + y,
        middleware: [
          (ctx, nxt) => {
            console.log(ctx.request.body)
            return nxt()
          }
        ]
      },
      middleware: [koaBody()]
    },
    [
      '/:x/:y',
      {
        any: {
          handler: ({
            pathParams: { x, y },
            query: { z = 0 },
            body: { q = 0 }
          }) => +x + +y + +z + +q
        }
      }
    ]
  ],
  ['/', 'hello']
])

app.use(routes)

app.listen(8080)

Readme

Keywords

Package Sidebar

Install

npm i @tofurama3000/tree-router

Weekly Downloads

20

Version

0.0.17

License

MIT

Unpacked Size

283 kB

Total Files

10

Last publish

Collaborators

  • tofurama3000