Reputation: 1144
Is there a difference between
app.use('/some/path', function(req, res, next() {})
and
app.all('/some/path', function(req, res, next() {})
They are both middleware functions that get called for /some/path requests only, right?
Upvotes: 27
Views: 6326
Reputation: 4468
There is big difference between the use of these two examples. Functions registered with app.use
are general middleware functions and is called appropriate to their position on the middleware stack, typically inside an app.configure
function. This type of middleware is usually placed before app.route
, with the exception of error handling functions.
On the other hand app.all
is a routing function (not usually called middleware) which covers all HTTP methods and is called only inside app.route
. If any of your previous router function matches the /some/path
and did not call the next
callback, app.all
will not be executed, so app.all
functions are usually on the beginning of your routing block.
There is also third type of middleware, used in your routing functions, eg.
app.get('/some/path', middleware1, middleware2, function(req, res, next) {});
which is typicaly used for limiting access or perform general tasks related to /some/path
route.
For practical application you can use both functions, but be careful of the difference in behaviour when using app.use
with /some/path
. Unlike app.get
, app.use
strips /some/path
from the route before invoking the anonymous function.
You can find more in the documentation of express.
Upvotes: 25