path在app.use()和app.get()
当我在/foo
上做GET请求时,我的请求是通过例A中的第一个中间件函数传递的,但是在例B中跳过了吗?
例子A
GET'/ 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
GET'/ 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等)使用指定的path。 具体app.use
:
在指定的path上装载指定的中间件function或中间件function:当请求的path的基础与path匹配时,执行中间件function。
虽然app.get()
指示它只使用该特定方法(GET)的path。
使用指定的callback函数将HTTP GET请求路由到指定的path。