Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drop support of passing prefix to .middleware/.legacyMiddleware #8

Closed
tunnckoCore opened this issue Oct 21, 2016 · 0 comments
Closed

Comments

@tunnckoCore
Copy link
Owner

tunnckoCore commented Oct 21, 2016

Basically it works correctly. But later if you wanna list them all you can't get correct and actual routes.

If you have, for example, one router and use .middleware multiple times with different prefixes all will work, but you can't get all available routes - they works, but you can list them for example, after the server is started (for example list them from the app.listen(port, fn) callback)

let Router = require('koa-rest-router')
let router = Router()

router.resource('users').resource('cats')

let Koa = require('koa')
let app = new Koa()

app.use(router.middleware())
app.use(router.middleware({ prefix: '/api/v1' }))
app.use(router.middleware({ prefix: '/foo/api/v3' }))

router.routes.forEach((route, idx) => {
  console.log(idx + 1, route.path)
})

It will print only 14 routes, only these that are on / prefix, but not the others. They will work. If you access them through web. You will have 42 actual and working routes, but it will output only 14 of them.

So if you wanna do the trick correctly you should do 3 different routers and use the .extend method.

let router2 = Router({ prefix: '/api/v1' })
let router3 = Router({ prefix: '/foo/api/v3' })

// add `router`'s routes on `router2`
router2.extend(router)

// add `router`'s routes on `router3`
router3.extend(router)

// add the three routers on koa server
app.use(router.middleware())
app.use(router2.middleware())
app.use(router3.middleware())

let i = 1

router.routes.forEach((route) => console.log(i++, route.path))
router2.routes.forEach((route) => console.log(i++, route.path))
router3.routes.forEach((route) => console.log(i++, route.path))
tunnckoCore pushed a commit to tunnckoCore/koa-better-router that referenced this issue Oct 21, 2016
It was working, basically. But leads to more shits than goods. Explicit is better.

BREAKING CHANGE: `.middleware` method no more accepts arguments - use `.legacyMiddleware` for

legacy; create a new router for another prefix then use `.extend` method for grouping the router.

Closes #9 and tunnckoCore/koa-rest-router#8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant