是不是可以为一个Express端点使用多个中间件函数?

huangapple go评论67阅读模式
英文:

Is it possible to use multiple middleware functions for just one express endpoint?

问题

可以,你可以像这样编写带有两个中间件函数(authenticateToken 和 authenticateAdminKey)的代码:

app.post('/api', authenticateToken, authenticateAdminKey, function() {
  // ...
});

我知道当你为所有端点使用多个中间件时,类似的方式是可行的(如这里所提到的)。但是我想知道是否可以仅对一个端点实现类似的功能?

英文:

Basically, is it possible to write code like this with two middleware functions (authenticateToken and authenticateAdminKey) ?

app.post('/api', authenticateToken, authenticateAdminKey, function() {
 ...
}

I know something similar is possible when you're using multiple middleware for all the endpoints (as mentioned here). But I wanted to know if something like this is possible for just one endpoint?

答案1

得分: 4

中间件列表以数组形式传递

app.post('/api', [authenticateToken, authenticateAdminKey], function() {
    // ...
}
英文:

middleware list pass in array

app.post('/api', [authenticateToken, authenticateAdminKey], function() {
 ...
}

huangapple
  • 本文由 发表于 2020年1月3日 21:28:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定