7
app.all('*', function(req, res, next) { 

vs

app.use(function (req, res, next) {

Whats the difference? doesn't both take in each request to the server?

sdfdsf sdf
  • 383
  • 1
  • 5
  • 12
  • Does this answer your question? [Difference between app.all('\*') and app.use('/')](https://stackoverflow.com/questions/14125997/difference-between-app-all-and-app-use) – Oliver Sieweke Mar 02 '21 at 20:36

1 Answers1

13

For the wildcard * path, there's really not much of a meaningful difference at all. It appears to me like the internal implementation may be slightly more efficient for app.use(fn), then app.all('*', fn). And, if you intend for it to run on all routes, then app.use() makes more logical sense to me since what you're really doing is middleware and that's what app.use() is specially designed for.

Summary for app.all('*', fn) vs. app.use(fn):

  1. No difference in order of execution.
  2. app.use() fires regardless of methods, app.all() only fires for parser supported methods (probably not relevant since the node.js http parser supports all expected methods).

Summary for app.all('/test', fn) vs. app.use('/test', fn):

  1. No difference in order of execution
  2. app.use() fires regardless of methods, app.all() only fires for parser supported methods (probably not relevant since the node.js http parser supports all expected methods).
  3. app.use() fires for all paths that start with /test include /test/1/ or /test/otherpath/more/1. app.all() only fires if its an exact match to the requested url.

Details

All route handlers or middleware that match a given route are executed in the order they were defined so app.all('*', fn) and app.use(fn) do not have any different ordering when placed in identical places in the code.

In looking at the Express code for app.all() it appears that the way it works is that it just goes through all the HTTP methods that the locally installed HTTP parser supports and registers a handler for them. So, for example, if you did:

app.all('*', fn);

The Express code would run these:

app.get('*', fn);
app.put('*', fn);
app.post('*', fn);
app.delete('*', fn);
// ...etc...

Whereas app.use() is method independent. There would be only one handler in the app router's stack that is called no matter what the method is. So, even if an unsupported http verb was issued and the parser let the request get this far, the app.use() handler would still apply whereas the app.all() handler would not.


If you use a path with both app.all() and app.use() that is not just a simple wildcard like '*', then there is a meaningful difference between the two.

app.all(path, fn) only triggers when the requested path matches the path here in its entirety.

app.use(path, fn) trigger when the start of the requested path matches the path here.

So, if you have:

app.all('/test', fn1);     // route 1
app.use('/test', fn2);     // route 2

And, you issue a request to:

http://yourhost.com/test         // both route1 and route2 will match
http://yourhost.com/test/1       // only route2 will match

Because only middleware with app.use() fires for partial matches where the requested URL is contains more path segments beyond what is specified here.


So, if you intend to insert some middleware that runs for all routes or runs for all routes that are descended from some path, then use app.use(). Personally, I would only use app.all(path, fn) if I wanted a handler to be run only for a specific path no matter what the method was and I didn't not want it to also run for paths that contain this path at the start. I see no practical reason to ever use app.all('*', fn) over app.use(fn).

jfriend00
  • 683,504
  • 96
  • 985
  • 979