0

I have an KOA endpoint. I have a quantify param that can only accept numbers, how can I enforce this directly in the KOA router?

.put('/cart/:product/:quantity', async ctx => {
    quantity = ctx.params.quantity;
    ctx.body = 'my code here';
}
rat
  • 1,277
  • 16
  • 24

1 Answers1

1

Use this regexp:

'/cart/:product/:quantity(\\d+)'

^ matches quantities that only consist of numbers. \d+ is the regexp, but you have to add another \ for the router to convert it into a proper regexp since the route is a string.

Jonathan Ong
  • 19,927
  • 17
  • 79
  • 118