Skip to content

Router.match is returning pathAndMethod matches for layers without methods such as nested routers or middleware #105

@Gary-Osteen-Q2

Description

@Gary-Osteen-Q2

Description
When using nested routers and other middleware, the Router.match can include layers without methods in the matched.pathAndMethod array. This can cause issues downstream when setting the ctx._matchedRouteName, causing named routes to not be set properly. The assumption being that the last item in the pathAndMethod array being the most specific layer.

node.js version: v12.18.3
npm/yarn and version: 6.14.6
@koa/router version: 9.4.0
koa version: 2.13.0

Code sample:

const Koa = require('koa')
const Router = require('@koa/router')

const app = new Koa()

const router = new Router()
router.get('main#info', '/info', function() {})

const nestedRouter = new Router()
nestedRouter.get('nested#name', '/updates', function() {})

router.use('/v1/api', nestedRouter.routes(), nestedRouter.allowedMethods())

app.use(router.routes()).use(router.allowedMethods())

app.listen(8084)

Expected Behavior:

Only layers with path and methods should be included in matched.pathAndMethods array.

Actual Behavior:

Any layer which matches the given path will appear in matched.pathAndMethods array. Even if the layer has zero methods associated with them.
Screen Shot 2020-09-23 at 2 57 15 PM

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions