router: Routing does not handle optional route parameter with prefix

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

About this issue

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

Most upvoted comments

@niftylettuce ā˜šŸ½