question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Gary-Osteen-Q2commented, Sep 23, 2020

I figured it out. First time contributing to open source. Learned a lot.

0reactions
dominiceggintoncommented, Oct 30, 2020

@jmealo I have already approved #106, just waiting on a core contributor to take a look and possibly merge.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Router in axum::routing - Rust - Docs.rs
Nest a group of routes (or a Service ) at some path. This allows you to break your application into smaller pieces and...
Read more >
Vert.x Web
Some of the key features of Vert.x-Web include: Routing (based on method, path, etc). Regular expression pattern matching for paths.
Read more >
Migrating to React Router v6: A complete guide
Migrate your React Router applications from v5 to v6 with this in-depth guide ... For nested routes, developers had to find the entire...
Read more >
Router middleware for koa. Provides RESTful resource routing.
function koa-router.layer (path, methods, middleware, opts) ... `Route#match` are now pure functions that return an array of routes that match the URL path....
Read more >
The Guide to Nested Routes with React Router - ui.dev
The mental model I use for Route is that it always has to render something – either its element prop if the path...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found