简体   繁体   中英

How to match path with regexp with path-to-regexp?

I have path to get user by his id:

/user/{id}

Then I transform it to regexp:

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

And then compare it with

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

But when I go to /user/count it thinks that I am going to /user/{id} .

It will assume whatever you are passing as a parameter. If the same HTTP verb has been used. GET user/{id} --> GET use/regEx(anything) :: It consider regEx(anything) as parameter.

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::

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM