首页 文章

如何使用数字创建KOA路径?

提问于
浏览
0

我有一个KOA endpoints . 我有一个只能接受数字的 quantify 参数,我如何直接在KOA路由器中强制执行此操作?

.put('/cart/:product/:quantity', async ctx => {
    quantity = ctx.params.quantity;
    ctx.body = 'my code here';
}

1 回答

  • 0

    使用此正则表达式:

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

    ^匹配仅包含数字的数量 . \d+ 是正则表达式,但您必须为路由器添加另一个 \ ,以将其转换为正确的正则表达式,因为路由是一个字符串 .

相关问题