0

I'm confused about path in express.use. In express document https://expressjs.com/en/4x/api.html#app.use

it said: Path:This will match paths starting with /abcd:

app.use('/abcd', function (req, res, next) {
next()

})

Does this mean the '/abcd' in app.use work like regex?, which will match any url starting with /abcd (like /abcd/efg) Then, it will somehow "remove" the /abcd part in the above url, and pass "/efg" to the middleware function? (by middleware, I am referring to the router example here app.use('/birds', birds) https://expressjs.com/en/guide/routing.html)

If so, is express.router.use(), router.get(), App.get() works the same way?

Li Zeng
  • 1
  • 1
  • It simply means that if the path starts with `/abcd`, then the given middleware function will be called (unless prior middleware has already handled the request). A router is just a middleware function that inspects the request and decides which of its child routes, if any, to match. The "removal" of the /abcd part is something that express.Router does to determine which of its child routes to match. It's not something that happens with all middleware. – Aurast Jul 22 '21 at 02:09
  • there is already an very good answer to your question at https://stackoverflow.com/a/15601856/3498612 – Tuan LE CONG Jul 22 '21 at 02:14
  • @Aurast Thank you, that's what I guess. I think the document can be more clear about the "removal" part and that App.use is "prefix-matching". Now I'm wondering if router.use() is also prefix-matching... – Li Zeng Jul 22 '21 at 02:16

0 Answers0