Closed antonpious closed 2 months ago
I know this question is a little old, but I wanted to answer in case someone else comes across this in a search.
Your example of method (and HTTP method) chaining is correct. If that's too verbose, you could use a router.all
and reject methods that you don't like. Example:
router.all('/foo', (ctx, next) => {
const allowed = ['GET', 'POST']
const isCorrect = allowed.includes(ctx.method)
ctx.assert(ctx, 405, "Ah nah man that's not right")
})
As for optional path segments: yes, and if you need more complicated options you can use more Regex. Anything supported by path-to-regexp should be supported here, with obvious caveats like needing extra escapes for some characters.
@koa/router
version: 10.1.1koa
version: 2.13.4Code sample:
In express route if we want to match multiple verbs with the same route we could provide the following syntax
What is the equivalent syntax in Koa Router? Currently its expecting the route to be repeated.
Does the router support optional parameters?