在Node.js上运行的Express.js中,app.all("*", … )
和app.use("/", … )
有什么有用的区别吗?
发布于 2013-01-03 01:06:43
是的,当使用任何类型的请求方法(POST、GET、PUT或DELETE)请求特定URI时,将调用app.all()
。
另一方面,app.use()
用于您可能拥有的任何中间件,它挂载到路径前缀上,并将在请求该路由下的URI时随时被调用。
这是app.all和app.use的文档。
发布于 2020-05-04 01:11:40
主要有两点不同:
模式匹配(Palani给出的答案)在使用app.use
加载的中间件的函数体中,next(route)
不起作用。文档中的链接说明了这一点:
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();});
https://stackoverflow.com/questions/14125997
复制相似问题