common-docs

common-docs

  • Docs
  • Koa related

›koa router related

koa router related

  • impress-router

impress-router

express style router for koa v2

Build Status Coverage Status node version npm version npm downloads npm license Greenkeeper badge

Install

npm i impress-router -S

basic usage

const App = require('koa')
const Router = require('impress-router')

const app = new App()
const router = new Router()
app.use(router)

new Router(opts)

opts

keytypedefaultdescription
strictBooleanfalsewhen false the trailing / is optional`, see path-to-regexp doc
sensitiveBooleanfalsewhen false, case is notsensitive, see path-to-regexp doc
mergeParamsBooleantruewhen true, merge params when we have nested routers
useThisBooleantruewhen true, the handlerthisequal toctx, like koa v1 does

router.use([path], middleware)

middleware can be mount on path, or on the root path /

arguments

nametyperequireddefaultdescription
pathStringfalse'/'the mount path
middlewareFunction / Array<Function>truethe handler

in the handler, ctx will have some extra prop attached

  • ctx.basePath the mount path
  • ctx.path the path with ctx.basePath trimed
  • ctx.originalPath the untrimed path

It's kind like Express req.baseUrl / req.originalUrl does

for Example

const App = require('koa')
const Router = require('impress-router')

const app = new App()
const router = new Router()
app.use(router)

router.use('/public', (ctx, next) {
  // when requesting `/public/js/foo.js`
  ctx.path // `/js/foo.js`
  ctx.basePath // `/public`
  ctx.originalPath // `/public/js/foo.js`
  return next()
})

when we request /public/js/foo.js

  • ctx.path will be /js/foo.js
  • ctx.basePath will be /public
  • ctx.originalPath will be /public/js/foo.js

router.method(path, handler)

  • get / post / ... methods exposed by methods module are supported
  • all method supported, via router.all(path,fn)
  • auto OPTIONS response, the router automatic build the Allow response
  • auto HEAD response

Example

const app = new (require('koa'))()
const router = new (require('impress-router'))()
app.use(router)

router.get('/hello', ctx => {
  ctx.body = 'world'
})

router.all('/foo', ctx => {
  ctx.body = 'bar'
})

ctx.params

const app = new (require('koa'))()
const Router = require('impress-router')
const router = new Router()
app.use(router)

const userRouter = new Router()
router.use('/user/:uid', userRouter)

userRouter.get('/:field', ctx => {
  ctx.body = {
    uid: ctx.params.uid,
    field: ctx.params.field,
  }
})

// GET /user/magicdawn/name
// =>
// { uid: 'magicdawn', field: 'name' }

router.augmentApp(app)

this method will augment the koa app instance, will

  • add app.use(path, middleware) support, the original app.use lack support of path
  • add app.get / app.post support

Why

  • express.Router is very nice
  • koa-router package is not very friendly, especially on middleware mount on path

so I'm porting it to koa

Changelog

CHANGELOG.md

License

the MIT License http://magicdawn.mit-license.org

  • Install
  • basic usage
  • new Router(opts)
  • router.use([path], middleware)
  • router.method(path, handler)
    • ctx.params
  • router.augmentApp(app)
  • Why
  • Changelog
  • License
common-docs
Docs
Getting Started (or other categories)Guides (or other categories)API Reference (or other categories)
Community
User ShowcaseStack OverflowProject ChatTwitter
More
BlogGitHubStar
Facebook Open Source
Copyright © 2019 magicdawn