app.all('*')和app.use('/')之间的区别

编程入门 行业动态 更新时间:2024-10-27 00:31:17
本文介绍了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('/')之间的区别

本文发布于:2023-11-23 19:31:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622620.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   app

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!