Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between app.all and app.use? [closed]

What is the difference between app.all() and app.use()?

like image 373
Preslav Rachev Avatar asked Dec 18 '12 17:12

Preslav Rachev


1 Answers

app.all() will be called when a particular URI is requested with any type of request method (e.g. GET, POST, PUT, or DELETE.)

app.use() on the other hand is more useful for any middleware you might have since it "mounts" onto a path prefix, and will be called anytime a URI under that route is requested. So for instance app.use('admin/', isAuthorized) could be a piece of middleware that will make sure any URI accessed under "admin/" (such as "admin/settings" or "admin/bing/bang") is being requested only by authorized users. If you don't provide a mount path, the default is to use the root URI "/".

You could do the same thing with app.all() by using a glob (e.g. app.all('admin/*', isAuthorized)), but app.use() does the extra step of stripping the mount URI from req.path which for some middleware is pretty useful.

like image 182
theabraham Avatar answered Oct 03 '22 00:10

theabraham