nestjs教程之@Controller()实现原理

@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 和方法匹配的路由,并调用相应的方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Young soul2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值