Skip to content

[Support] 请问如何使用 decorator 实现开放的 api,比如 @Public 作用在路由上,表示该路由无需验证身份 #3880

Answered by waitingsong
wjw-gavin asked this question in Q&A
Discussion options

You must be logged in to vote

核心是在每个请求时通过

const routeInfo = await this.webRouterService.getMatchedRouterInfo(ctx.path, ctx.method)
console.log(routeInfo.fullUrl)

拿到路由信息,然后挂载到 ctx 上面,后面的中间件、组件、装饰器都可以用了。
我把获取这个路由信息包装了下,内部有个缓存,可以极大减小相同接口后续请求的延迟:
首次请求(无缓存)vs 后续请求(有缓存)大约是 200:1 的开销。接口越多比例应该越大。

Replies: 4 comments 14 replies

Comment options

You must be logged in to vote
2 replies
@waitingsong
Comment options

@wjw-gavin
Comment options

Comment options

You must be logged in to vote
3 replies
@wjw-gavin
Comment options

@waitingsong
Comment options

@wjw-gavin
Comment options

Comment options

You must be logged in to vote
2 replies
@waitingsong
Comment options

@waitingsong
Comment options

Comment options

You must be logged in to vote
7 replies
@wjw-gavin
Comment options

@waitingsong
Comment options

@wjw-gavin
Comment options

@waitingsong
Comment options

Answer selected by wjw-gavin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
type: question / discussion This issue or pull request need be discussion
3 participants