5

Is there a difference between

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

});

and...

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

});
hyubs
  • 737
  • 6
  • 18
  • 2
    Possible duplicate of [Difference between app.all('\*') and app.use('/')](http://stackoverflow.com/questions/14125997/difference-between-app-all-and-app-use) – Dan Nissenbaum Jun 03 '16 at 13:31

2 Answers2

5

app.all() references the application router like post or get, while app.use() simply references the applications middleware. app.use() is better for more globally defined statements that you want persistent through your entire application.

Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
  • 3
    `app.all(*)` actually loops through all HTTP methods (from the 'methods' npm package) and does a `app.('*', function (req, res, next) {..})` for each HTTP method. – mscdex May 25 '14 at 02:27
-2

app.use takes only one callback function and its meant for Middleware. Middleware usually don't handle request and response, (technically they can) they just process input data, and hand over it to next handler in queue.

app.use([path], function) app.all take multiple callbacks, and meant for routing. with multiple callbacks you can filter requests and send responses. Its explained in Filters on express.js

app.all(path, [callback...], callback)

mahesh
  • 1