pillarjs / path-to-regexp

Turn a path string such as `/user/:name` into a regular expression
MIT License
8.18k stars 382 forks source link

params are being joined without `/` #299

Closed brc-dd closed 1 year ago

brc-dd commented 1 year ago
const { compile } = require('path-to-regexp')

const toPath = compile(':splat*')

console.log(toPath({ splat: ['foo', 'bar.md'] })) // 'foobar.md'

const toPath_working = compile('/:splat*')

console.log(toPath_working({ splat: ['foo', 'bar.md'] })) // '/foo/bar.md'

Is the behavior in first case expected? I think it should've yielded 'foo/bar.md' 👀

blakeembrey commented 1 year ago

Unfortunately it is expected. The route :splat* does not match /foo/bar.md, only the second one would. Since the idea is to rebuild a URL that would match correctly, this is the expected behavior.

It stands to reason that :splat* could just be wrong too. There's no option in the library today for "implicitly use / as the prefix", but there could be. And that might solve your issue. But I'd be inclined to suggest using the / prefix explicitly unless there's a good reason you can't.