@Controller实现原理的简单实现,在真实的应用中,路由处理会更加复杂,包括错误处理、中间件支持、参数解析等
import 'reflect-metadata';
function Controller(prefix: string = '') {
return function (constructor: Function) {
Reflect.defineMetadata('prefix', prefix, constructor);
// 可以附加更多元数据,例如用于路由的方法和路径
};
}
function Get(path: string): MethodDecorator {
return function (target, propertyKey) {
Reflect.defineMetadata('route', {
method: 'GET', path }, target, propertyKey);
};
}
function Post(path: string): MethodDecorator {
return function (target, propertyKey) {
Reflect.defineMetadata('route', {
method: 'POST', path }, target, propertyKey);
};
}
@Controller('/users')
class UserController {
@Get('/')