这两种使用快速中间件的方法之间有区别吗? [英] Is there a difference between these two methods of using express middleware?
问题描述
我遇到了两种不同的方式来定义express,use()中间件,并且想知道它们之间是否有任何区别,或者仅仅是语法糖?
I have come across two different ways to define express, use() middleware and am wondering if there is any difference between them or if it is simply syntax sugar?
const app = express();
app.use(cors());
app.use(responseTime());
app.use(someFunction);
app.use(anotherHandler);
app.use(failureHandler);
B
const app = express();
app.use(cors())
.use(responseTime())
.use(someFunction)
.use(anotherHandler)
.use(failureHandler);
推荐答案
它们不是使用它的两种方式.它们是相同的方式.通过调用 app.use()
.
They are not two ways of using it. They are the same way. By calling app.use()
.
它也不是语法糖. app.use()
返回 this
,因此它返回的值(与 app
中存储的值相同)可用于链接另一个 .use()
的调用,依此类推.
结果是一样的;选择哪种方式取决于您的口味(应该使用一种您认为更易于阅读和理解的方式).
And it is not syntactic sugar either. app.use()
returns this
, hence the value it returns (which is the same value stored in app
) can be used to chain another call of .use()
and so on.
The result is the same; it's a matter of taste which way you prefer (you should use the one that you feel it's easier to read and understand).
这篇关于这两种使用快速中间件的方法之间有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!