簡體   English   中英

如何將路徑與正則表達式與路徑到正則表達式匹配?

[英]How to match path with regexp with path-to-regexp?

我有通過他的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}

它將假定您要傳遞的任何參數。 如果使用了相同的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”->將運行::

否則,兩者都將運行:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM