我有通过id获取用户的路径:
/user/{id}
然后我将其转换为regexp:
pathToRegexp(path.replace(/\{/g, ':').replace(/\}/g, ''))
然后将其与
const matchedPaths = this.map.filter((obj: mapper) =>
requestUrl.match(obj.regexp)
)[0]
但当我转到/user/count
时,它会认为我要转到/user/{id}
。
发布于 2019-05-22 03:09:54
它将假定您传递的任何内容都是参数。如果使用了相同的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$/')
})
仅"/: id“->将运行::
否则两者都将运行::
https://stackoverflow.com/questions/56241005
复制相似问题