Node.JS Express中的app.all('*', ... )
和app.use('/', ...)
之间是否有用?
答案 0 :(得分:112)
在大多数情况下,他们会等效地工作。最大的区别在于应用中间件的顺序:
app.all()
附加到应用程序的路由器,因此无论何时到达app.router中间件(处理所有方法路由... GET,POST等)都会使用它。
app.use()
附加到应用程序的主要中间件堆栈,因此它按照中间件指定的顺序使用。例如,如果你把它放在第一位,它将是第一个运行。如果你把它放在最后,(在路由器之后),它通常根本不会运行。
通常,如果您想对所有路由进行全局操作,app.use()是更好的选择。此外,它具有更少的未来错误机会,因为快递0.4可能会丢弃隐式路由器(意思是,中间件中路由器的位置将比现在更重要,因为您在技术上甚至不必使用它现在)。
答案 1 :(得分:72)
app.use 只需要一个回调函数,它适用于中间件。中间件通常不处理请求和响应,(技术上他们可以)他们只处理输入数据,并将其移交给队列中的下一个处理程序。
app.use([path], function)
app.all 需要多次回调,并且用于路由。通过多个回调,您可以过滤请求并发送响应。它在Filters on express.js
中解释app.all(path, [callback...], callback)
app.use 仅查看url是否以指定的路径
开头app.use( "/product" , mymiddleware);
// will match /product
// will match /product/cool
// will match /product/foo
app.all 将匹配完整路径
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
答案 2 :(得分:14)
app.use:
app.all:
看看这个expressJs代码示例:
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);
这是访问路线'/ hello'时的日志:
(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
答案 3 :(得分:11)
使用app.use()
,“mount”路径被剥离,中间件函数不可见:
app.use('/static', express.static(__dirname + '/public'));
除非express.static
包含此前缀(req.url
),此时它被剥离,否则不会调用已装入的中间件函数(/static
)被调用。
使用app.all()
,没有这种行为。
答案 4 :(得分:3)
是的,当使用任何类型的请求方法(POST,GET,PUT或DELETE)请求特定URI时,app.all()
被调用
另一方面,app.use()
用于您可能拥有的任何中间件,并且它会安装到路径前缀上,并且只要请求该路径下的URI,就会调用它。
答案 5 :(得分:1)
上面提到的两个区别都不重要。
第一个:app.all
接受一个正则表达式作为其路径参数。 app.use
不接受正则表达式。
第二个:
app.all(path,handler)
或app[method](path,handler)
,处理者的path
必须与所有人 path
相同。这是app [method]的路径已完成。
app.use(path,hanlder)
,如果use的路径是完整的,则处理程序的路径必须为'/'。如果use的路径是完整路径的开始,则处理程序路径必须是其余完整路径。
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');
});
答案 6 :(得分:1)
有两个主要区别:
1.模式匹配(Palani提供的答案)
2. next(route)
在使用app.use
加载的中间件的函数体内不起作用。在文档的链接中对此进行了说明:
NOTE: next('route') will work only in middleware functions that were loaded by using the app.METHOD() or router.METHOD() functions.
链接:http://expressjs.com/en/guide/using-middleware.html
next('route')
的工作效果可以从以下示例中看到:
app.get('/',
(req,res,next)=>{console.log("1");
next(route); //The code here skips ALL the following middlewares
}
(req,res,next)=>{next();}, //skipped
(req,res,next)=>{next();} //skipped
);
//Not skipped
app.get('/',function(req,res,next){console.log("2");next();});