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';
}
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';
}
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.