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

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
SpringBoot 是基于 Spring 框架开发的快速开发框架。其中 `@Controller` 注解是使用 Spring 框架开发 web 应用的重要组成部分之一。它主要功能是定义控制器,处理 http 请求和响应。下面是 `@Controller` 注解的原理: 1. `@Controller` 注解的作用 `@Controller` 是 Spring 框架的一个注解,用于标识一个类是控制器组件。它的作用是将一个普通的 Java 类标记为 Spring MVC 的控制器组件,告诉 Spring 框架它应该被作为一个控制器来处理请求。 2. Spring MVC 控制器原理 Spring MVC 框架的核心是前端控制器 DispatcherServlet。当请求进入 DispatcherServlet 后,它会根据请求的 URL 去寻找匹配的控制器映射,然后再将请求转发到对应控制器的处理方法。控制器接收到请求后进行处理,然后返回 ModelAndView 对象,将数据和视图返回给 DispatcherServlet。 3. `@Controller` 注解的实现 `@Controller` 注解的实现是基于 Spring IoC 容器。在 Spring IoC 容器启动后,用 `@Controller` 注解注解的类会被自动扫描并注册到 Spring 容器中。然后在 DispatcherServlet 中,通过调用 Spring 容器中的 Bean 执行对应的控制器操作。 4. `@Controller` 注解与其他注解的区别 `@Controller` 注解是 SpringMVC 中最为重要的注解之一。相比其他注解,如 `@RestController`、`@RequestMapping` 等,`@Controller` 更为普遍。它不仅可以用在基于 SpringBoot 的项目中,也可以应用在基于 Spring 框架的其他 Web 项目中。 5. 总结 总之,`@Controller` 注解是 SpringMVC 框架中定义控制器组件的关键注解。它的使用需要基于 Spring IoC 容器的构建和调用,同时与 DispatcherServlet 紧密关联,共同构建了一个完整的 SpringMVC 框架来处理请求和响应。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Young soul2

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

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

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

打赏作者

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

抵扣说明:

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

余额充值