为什么当我在/foo
上做GET请求时,我的请求通过示例a中的第一个中间件函数,但在示例B中绕过它?
'/foo"
app.use('/', function(req, res, next) {
console.log("req passes through here");
next();
}
app.get('/foo', function(req, res, next) {
console.log("then req passes through here");
}
例B
'/foo"
app.get('/', function(req, res, next) {
console.log("this part is bypassed...");
next();
}
app.get('/foo', function(req, res, next) {
console.log("then req passes through here");
}
app.use()和app.get()使用相同的path参数。
那么为什么安装在/
上的中间件在示例B中没有执行?
app.use()
指示应用程序对所有调用的所有方法(GET, PUT, POST等)使用指定的路径。特别是app.use
:
在指定的路径上挂载指定的中间件函数或函数:当请求的路径的基与path匹配时,执行中间件函数。
而app.get()
指示它仅为该特定路径使用特定方法(GET)的路径。
使用指定的回调函数将HTTP GET请求路由到指定的路径。