gpt4 book ai didi

javascript - 如何将路径与正则表达式与路径到正则表达式相匹配?

转载 作者:太空宇宙 更新时间:2023-11-04 01:30:43 25 4
gpt4 key购买 nike

我有通过 ID 获取用户的路径:

/user/{id}

然后我将其转换为正则表达式:

pathToRegexp(path.replace(/\{/g, ':').replace(/\}/g, ''))

然后与它进行比较

const matchedPaths = this.map.filter((obj: mapper) =>
requestUrl.match(obj.regexp)
)[0]

但是当我转到 /user/count 时,它认为我要去 /user/{id}

最佳答案

它将假定您作为参数传递的任何内容。如果使用了相同的 HTTP 动词。 GET user/{id} --> GET use/regEx(anything)::它将 regEx(anything) 视为参数。

app.get('/:id', function (req, res) {
res.send(req.params.id)
})

app.get(/.*fly$/, function (req, res) {
res.send('/.*fly$/')
})

app.get(/.*plane$/, function (req, res) {
res.send('/.*plane$/')
})

only "/: id" -> Will run ::

other wise both will run::

关于javascript - 如何将路径与正则表达式与路径到正则表达式相匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56241005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com