app.all('*')和app.use('/')之间的区别 [英] Difference between app.all('*') and app.use('/')

查看:213
本文介绍了app.all('*')和app.use('/')之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

app.all('*',...) app.use('/',。 ..)在Node.JS Express?

Is there a useful difference between app.all('*', ... ) and app.use('/', ...) in Node.JS Express?

推荐答案

在大多数情况下,他们将工作等效。最大的区别是应用中间件的顺序:

In most cases they would work equivalently. The biggest difference is the order in which middleware would be applied:


  • app.all() / code>附加到应用程序的路由器,所以它被用于每当app.router中间件到达(它处理所有的方法路由... GET,POST等)。

  • app.all() attaches to the application's router, so it's used whenever the app.router middleware is reached (which handles all the method routes... GET, POST, etc).

app.use()附加到应用程序的主中间件堆栈,所以按照中间件指定的顺序使用。例如,如果你把它放在第一位,这将是第一件事情。如果你把它放在最后(路由器之后),通常根本就不会运行。

app.use() attaches to the application's main middleware stack, so it's used in the order specified by middleware. eg, if you put it first, it will be the first thing run. If you put it last, (after the router), it usually won't be run at all.

如果您想在全球范围内为所有路线做某事,那么app.use()是更好的选择。此外,它的未来错误的机会也较少,因为express 0.4可能会丢弃隐式路由器(这意味着,路由器在中间件中的位置将比现在更重要,因为您在技术上甚至不需要使用它现在)。

Usually, if you want to do something globally to all routes, app.use() is the better option. Also, it has less chance of future bugs, since express 0.4 will probably drop the implicit router (meaning, the position of the router in middleware will be more important than it is right now, since you technically don't even have to use it right now).

这篇关于app.all('*')和app.use('/')之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆