expressjs / express

Fast, unopinionated, minimalist web framework for node.

Home Page:https://expressjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there any subtle difference between use() / * []

wll8 opened this issue · comments

commented
// 1
app.use(fn)

// 2
app.use(`/`, fn)

// 3
app.use(`*`, fn)

// 4
app.use([], fn)

there is no difference because both will prevent the request to go beyond them.

commented

I remember I found their nuances in some project, but I forgot. I will continue to keep an eye on this issue

Hi @wll8 ! So the app.use function takes basically n-paths and then n-middlewares. When no paths are given, the default is '/'. This means that 1, 2, and 4 are all exactly the same in your example. But 3 uses the path *, which is a capture group, so though matches the same URLs, the entire URL is captured in req.params[0] and then the matched part (the entire URL) is trimmed off from req.url, leaving it as just /.

I hope that helps!