【发布时间】:2021-11-02 08:01:23
【问题描述】:
我是nestjs 的新手,我想同时在我的控制器中的一个方法上应用Get 和Post。
为简单起见,我只是贴出核心逻辑代码sn-p:
定制装饰器
import { Get, Post } from "@nestjs/common";
import { RenderReact } from 'my-personal-package';
export function Page(path: string, view?: React.ComponentType, methodDecorators?: ((path?: string | string[]) => MethodDecorator)[]): MethodDecorator {
return (target: any, key: string, desc: PropertyDescriptor) => {
const decorators = [
Get(path), // Add Get first.
Post(path) // Add Post then.
];
if (view) {
decorators.push(RenderReact(view)); // RenderReact will return a MethodDecorator as well.
}
decorators.forEach(decorate => decorate(target, key, desc));
return desc;
};
}
控制器方法:
@Page("my-path", ThisIsMyPageFunctionalComponent, [Post]) // Post was from @nestjs/common
async return() {
// method logic
}
Page函数最开始的数组“装饰器”,
- 添加Get,然后Post,只有Post有效。
- 添加Post,然后Get,只有Get有效。
我们如何在这里同时应用 Get/Post?
【问题讨论】:
-
我认为由于 HTTP 装饰器工厂的工作方式,您不能这样做。 Here 你可以看到只有最后一个元数据附加到目标方法。也许有办法做到这一点,但不使用
Get和Post
标签: node.js reactjs frontend nestjs web-frontend