The difference between app.all ('*') and app.use ('/')

Is there a useful difference between app.all('*', ... ) and app.use('/', ...) in Node.JS Express?

+104
express
Jan 02 '13 at 17:00
source share
6 answers

In most cases, they will work the same way. The biggest difference is the order of application of the middleware:

  • app.all() connects to the application router, so it is used whenever the app.router middleware is reached (which processes all the routes of the method ... GET, POST, etc.).

ATTENTION: app.router is deprecated in Express 4.x

  • app.use() joins the main middleware stack of the application, so it is used in the order specified by the middleware. For example, if you put it first, it will start first. If you put it last (after the router), it usually does not start at all.

Usually, if you want to do something globally for all routes, it is better to use app.use (). In addition, it has less chance of future errors, since Express 0.4 is likely to drop the implicit router (this means that the position of the router in the middleware will be more important than now, because technically you do not even need to use it). right now).

+112
Jan 02 '13 at 17:06
source share

app.use accepts only one callback function and is designed for Middleware. The middleware usually does not process requests and responses (technically they can), they simply process the input data and pass it to the next handler in the queue.

 app.use([path], function) 

app.all accepts several callbacks and is intended for routing. with multiple callbacks, you can filter requests and send responses. This is explained in filters on express.js

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

app.use only sees if the URL begins with the specified path

 app.use( "/product" , mymiddleware); // will match /product // will match /product/cool // will match /product/foo 

app.all will match the full path

 app.all( "/product" , handler); // will match /product // won't match /product/cool <-- important // won't match /product/foo <-- important app.all( "/product/*" , handler); // won't match /product <-- Important // will match /product/ // will match /product/cool // will match /product/foo 
+73
Jun 12 '14 at 19:04
source share
  • app.use:

    • type middlware on your controller, for example: header, cookies, sessions, etc.
    • must be written before the application [http_method], otherwise it will not be executed.
    • multiple calls are handled in spelling order
  • app.all:

    • (for example, the [http_method] application) is used to configure route controllers.
    • "all" means that it applies to all http methods.
    • multiple calls are handled in spelling order

Take a look at this example expressJs code:

 var express = require('express'); var app = express(); app.use(function frontControllerMiddlewareExecuted(req, res, next){ console.log('(1) this frontControllerMiddlewareExecuted is executed'); next(); }); app.all('*', function(req, res, next){ console.log('(2) route middleware for all method and path pattern "*", executed first and can do stuff before going next'); next(); }); app.all('/hello', function(req, res, next){ console.log('(3) route middleware for all method and path pattern "/hello", executed second and can do stuff before going next'); next(); }); app.use(function frontControllerMiddlewareNotExecuted(req, res, next){ console.log('(4) this frontControllerMiddlewareNotExecuted is not executed'); next(); }); app.get('/hello', function(req, res){ console.log('(5) route middleware for method GET and path patter "/hello", executed last and I do my stuff sending response'); res.send('Hello World'); }); app.listen(80); 

Here is the log when accessing the '/ hello' route:

 (1) this frontControllerMiddlewareExecuted is executed (2) route middleware for all method and path pattern "*", executed first and can do stuff before going next (3) route middleware for all method and path pattern "/hello", executed second and can do stuff before going next (5) route middleware for method GET and path patter "/hello", executed last and I do my stuff sending response 
+14
Nov 13 '13 at 13:48
source share

With app.use() "mount" path is split and not displayed in the middleware function:

 app.use('/static', express.static(__dirname + '/public')); 

Installed middleware functions ( express.static ) are not called unless req.url contains this prefix ( /static ) , at which point it is disabled when the function is called.

With app.all() this behavior is not.

+11
Mar 19 '13 at 6:28
source share

Yes, app.all() is called when a specific URI is requested with any type of request method (POST, GET, PUT or DELETE)

On the other hand, app.use() used for any middleware that you may have, and it is mounted on a path prefix and will be called at any time when a URI is requested on this route.

Here is the documentation for app.all and app.use .

+3
Jan 02 '13 at 17:06
source share

Two differences in all of the above answers do not matter.

First: app.all accepts a regular expression as a path parameter. app.use does NOT accept regex.

Second: app.all(path,handler) or app[method](path,handler) , path handler should be the same with the whole path . This is the app [method] 'path completed.

app.use(path,hanlder) , if the usage path is completed, the hanlder path must be equal to '/'. If the use path is the beginning of the full path, the handler path should be the rest of the full path.

  app.use('/users', users); //users.js: the handler will be called when matchs '/user/' path router.get('/', function(req, res, next) { res.send('respond with a resource'); }); // others.js: the handler will be called when matchs '/users/users' path router.get('/users', function(req, res, next) { res.send('respond with a resource'); }); 



 app.all('/users', users); //others.js: the handler wil be called when matchs '/'path router.get('/', function(req, res, next) { res.send('respond with a resource'); }); //users.js: the handler will be called when matchs '/users' path router.get('/users', function(req, res, next) { res.send('respond with a resource'); }); 
+1
Dec 23 '18 at 11:12
source share



All Articles