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'); });
åĻå
ķå Dec 23 '18 at 11:12 2018-12-23 11:12
source share