Skip to content

Wildcard handlers not called if start of the path matches with a static path route #23

@opl-

Description

@opl-

Current behavior

Registering a middleware with a given path (for example /a) and a wildcard path (*) on the same Router instance will cause the wildcard middleware handler to not be called if the path we navigate to starts with the given path (for example /b will work, but /ab will return a 404 page).

Using a param handler instead of a wildcard middleware (.get('/:param', () => {...})) results in the same behavior.

Expected behavior

The wildcard middleware handler should be called if the path doesn't match.

Reproduction

const {Cottage} = require('cottage');
const Koa = require('koa');

const app = new Koa();
const router = new Cottage();

router.use('/a', (ctx) => {
	console.log(Date.now(), 'a', ctx.url);
	ctx.body = 'a';
});

// Path is implicitly '*'
// Replacing this with `router.get('/:param', ...)` also results in an error
router.use((ctx) => {
	console.log(Date.now(), 'wildcard', ctx.url);
	ctx.body = 'wildcard';
});

app.use(router.callback());

app.listen(8080);
  • Navigating to /a returns a
  • Navigating to /ab returns Not Found
  • Navigating to /b returns wildcard

Environment

Cottage: v3.1.3 (npm latest)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions