@Controller实现原理的简单实现,在真实的应用中,路由处理会更加复杂,包括错误处理、中间件支持、参数解析等
案例1
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('/')
getAllUsers() {
// 获取所有用户的逻辑
}
@Post('/')
createUser() {
// 创建用户的逻辑
}
}
function handleRequest(controllerClass: any, requestMethod: string, requestPath: string) {
const prefix = Reflect.getMetadata('prefix', controllerClass);
const controllerInstance = new controllerClass();
Object.getOwnPropertyNames(controllerClass.prototype).forEach(methodName => {
const route = Reflect.getMetadata('route', controllerClass.prototype, methodName);
if (route && route.method === requestMethod && (prefix + route.path) === requestPath) {
controllerInstance[methodName]();
}
});
}
// 模拟处理请求
handleRequest(UserController, 'GET', '/users/');
在实际的 NestJS 框架中,@Controller() 和路由装饰器的实现涉及到与框架其他部分的紧密集成,例如依赖注入容器、请求管道、中间件、拦截器等。
案例2
import 'reflect-metadata';
import * as http from 'http';
const ROUTE_METADATA_KEY = Symbol('route');
const ROUTES_METADATA_KEY = Symbol('routes');
interface RouteDefinition {
path: string;
requestMethod: 'GET' | 'POST' | 'PUT' | 'DELETE';
methodName: string;
}
function Controller(path: string = '') {
return function (target: Function) {
Reflect.defineMetadata(ROUTE_METADATA_KEY, path, target);
};
}
function createMethodDecorator(method: 'GET' | 'POST' | 'PUT' | 'DELETE') {
return function (path: string) {
return function (target, propertyKey: string | symbol) {
if (!Reflect.hasMetadata(ROUTES_METADATA_KEY, target.constructor)) {
Reflect.defineMetadata(ROUTES_METADATA_KEY, [], target.constructor);
}
const routes: RouteDefinition[] = Reflect.getMetadata(ROUTES_METADATA_KEY, target.constructor);
routes.push({ requestMethod: method, path, methodName: propertyKey.toString() });
};
};
}
const Get = createMethodDecorator('GET');
const Post = createMethodDecorator('POST');
@Controller('/users')
class UserController {
@Get('/')
getAllUsers(req, res) {
res.end('Get all users');
}
@Post('/')
createUser(req, res) {
res.end('Create a user');
}
}
const server = http.createServer((req, res) => {
const { url, method } = req;
// 简化的路由匹配逻辑
const controllers = [UserController]; // 添加更多控制器
for (const controller of controllers) {
const basePath = Reflect.getMetadata(ROUTE_METADATA_KEY, controller);
const routes: RouteDefinition[] = Reflect.getMetadata(ROUTES_METADATA_KEY, controller);
for (const route of routes) {
if (`${basePath}${route.path}` === url && route.requestMethod === method) {
const instance = new controller();
instance[route.methodName](req, res);
return;
}
}
}
res.statusCode = 404;
res.end('Not Found');
});
server.listen(3000, () => {
console.log('Server running on port 3000');
});
这个实现添加了对请求和响应对象的处理,并提供了一个基本的路由匹配逻辑。通过使用 @Get()、@Post() 等装饰器,可以为 UserController 的方法定义路由。当 HTTP 服务器接收到请求时,它会遍历注册的控制器和路由,查找与请求 URL 和方法匹配的路由,并调用相应的方法。