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.

Routing does not handle optional route parameter with prefix

See original GitHub issue

node.js version: 13.3.0

npm/yarn and version: npm 6.4.1 yarn 1.19.2 (this is what i’m using)

@koa/router version: 8.0.5

koa version: 2.11.0

Code sample:

const router = require('@koa/router')()
const Koa = require('koa')
// following line included to demonstrate difference in underlying library - version 6.1.0
const { pathToRegexp } = require("path-to-regexp")
const app = module.exports = new Koa()
const middleware = async (ctx, next) => { ctx.body = 'it worked'; return await next() }

const withoutIDRoute = '/foo{.:ext}?'
router.get('withoutID', withoutIDRoute, middleware)

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

console.log(pathToRegexp(withoutIDRoute).test('/foo.json')) // outputs true
console.log(pathToRegexp(withoutIDRoute).test('/foo')) // outputs true

app.listen(3080)

Expected Behavior:

  • http://localhost:3080/foo.json returns it worked as a text response
  • http://localhost:3080/foo returns it worked as a text response
  • More generally, @koa/router matches routes that path-to-regexp matches (as documented in this section)

Actual Behavior:

Both of the aforementioned URLs result in a 404 Not Found response

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:17 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
smartscommented, Jan 23, 2020

@niftylettuce ☝🏽

0reactions
niftylettucecommented, Jun 1, 2020

@smarts one moment, I’m going to have @3imed-jaberi look at that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Router with optional path parameter - Stack Overflow
My question is, can we declare it in one route? If I add only the second row then the route without the parameter...
Read more >
How to pass an optional parameter to a route? - Laracasts
I am not sure why the router does not deal with the query-parameter. I feel that it should be. The router should scan...
Read more >
Optional parameters in Web API Attribute Routing
In this article, I am going to discuss Optional Parameters in Web API Attribute Routing with some examples. You can make a URI...
Read more >
Routing - Laravel - The PHP Framework For Web Artisans
Basic Routing. Redirect Routes; View Routes; The Route List · Route Parameters. Required Parameters; Optional Parameters · Named Routes · Route Groups. Middleware ......
Read more >
Routing in Razor Pages
The framework has no way of knowing which page to call. You can disambiguate between routes by adding route parameters and/or constraints to ......
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