Node.js app.all(';*';)和app.use(';/';)之间的差异
在Node.JS Express中,Node.js app.all(';*';)和app.use(';/';)之间的差异,node.js,express,Node.js,Express,在Node.JS Express中,app.all('*',…)和app.use('/',…)之间是否存在有用的区别?在大多数情况下,它们的工作原理是相同的。最大的区别在于中间件的应用顺序: app.all() 注意:app.router在express 4.x中已被弃用 app.use()。如果你把它放在第一位,它将是第一个运行的东西。如果你把它放在最后(路由器之后),它通常根本不会运行 通常,如果您想对所有路由执行全局操作,app.use()是更好的选择。此外,由于express 0
app.all('*',…)
和app.use('/',…)
之间是否存在有用的区别?在大多数情况下,它们的工作原理是相同的。最大的区别在于中间件的应用顺序:
app.all()
app.use()。如果你把它放在第一位,它将是第一个运行的东西。如果你把它放在最后(路由器之后),它通常根本不会运行
app.all()
在使用任何类型的请求方法(POST、GET、PUT或DELETE)请求特定URI时调用
另一方面,app.use()
用于您可能拥有的任何中间件,它装载到路径前缀上,并将在请求该路由下的URI时被调用
以下是使用app.use()
的&.的文档,“装载”路径被剥离,中间件功能不可见:
app.use('/static', express.static(__dirname + '/public'));
除非req.url
包含此前缀(/static
),否则不会调用装入的中间件函数(express.static
)
对于app.all()
,没有这种行为。- app.use:
- 将middlware注入前端控制器配置,例如:头、cookie、会话等
- 必须在应用程序[http_method]之前写入,否则将不会执行李>
- 按写入顺序处理几个调用
- app.all:
- (如应用程序[http_方法])用于配置路由的控制器
- “all”表示它适用于所有http方法
- 按写入顺序处理几个调用
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
app.use只需要一个回调函数,它是用于中间件的。中间件通常不处理请求和响应,(从技术上讲,它们可以)它们只处理输入数据,并将其交给队列中的下一个处理程序
app.use([path], function)
app.all接受多次回调,用于路由。通过多个回调,您可以过滤请求并发送响应。其解释如下:
app.use仅查看url是否以指定路径开头
app.use( "/product" , mymiddleware);
// will match /product
// will match /product/cool
// will match /product/foo
应用程序。所有将匹配完整路径
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
app.all(“/product”,handler);
//将匹配/匹配产品
//不匹配/产品/酷两个差异以上所有答案都不匹配
第一个:app.all
接受正则表达式作为其路径参数<代码>应用程序。使用
不接受正则表达式
第二条:
app.all(path,handler)
或app[method](path,handler)
,处理程序的路径必须与所有的路径相同。这就是,应用程序[方法]的路径已完成
app.use(path,handler)
,如果use的路径是完整的,handler的路径必须是“/”。如果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');
});
有两个主要区别:
1.模式匹配(Palani给出的答案)
2. next(route)
在使用app.use加载的中间件的函数体内不起作用。这在文档的链接中有说明:
链接:
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();});
谢谢,但我认为你错过了应用程序。所有通配符和应用程序。使用根路径使它们几乎完全相同,不是吗?除了app.all可以接受一系列回调,app.use只能接受一个-对吗?这个问题明确询问app.use(“/”,…)。这是问题的正确答案,在2018年仍然正确!中间件也可以与all()一起装入。。。唯一的区别是,在执行中间件时,挂载路径被剥离。这在Express4.x之后仍然适用吗?app.router已被删除。您可以将next(“route”)
与app.all
一起使用,但不能与app.use一起使用。
@JozefMikusinec文档似乎另有建议。。。你的链接没有提到next('route'),但我查看了API,你是对的。@musicin3d我进一步研究并发现了这一点,这证实了“next()和next('route')对app.use没有区别”(引用)。他们应该更改文档。至少在中使用一个或多个中间件功能,而不是“仅一个”。app.use仅查看url是否以指定路径开始;app.all将匹配完整路径。这是主要区别。@frogcjn不应该,因为它忽略了我问题中的*和/。在express 4.x上逐字运行此示例后,它实际上按顺序运行了所有5个。这很可能是因为在写完这篇文章后的近3年里,express发生了变化,但我只是想为了清楚起见,我会添加这篇文章。
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');
});
NOTE: next('route') will work only in middleware functions that were loaded by using the app.METHOD() or router.METHOD() functions.
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();});