在Nest.js中访问嵌套路由
NestJS框架使用模块化设计,路由定义在控制器中。嵌套路由指的是在一个控制器中定义的路由路径,依赖于另一个控制器的路由路径。浏览器访问嵌套路由的方式与普通路由略有不同。
让我们来看一个例子:
假设我们有两个控制器:AppController和SystemController。
// app.controller.ts import { Controller, Get } from '@nestjs/common'; @Controller() // 根路由 export class AppController { @Get() root(): string { return 'Root route'; } }
// system.controller.ts import { Controller, Get } from '@nestjs/common'; @Controller('system') // 嵌套路由的父路径 export class SystemController { @Get('testindex') getTestIndex(): string { return 'Test index page'; } }
SystemController 使用 @Controller('system') 装饰器,定义了其路由前缀为 /system。 getTestIndex 方法的路由则相对于父路径 /system,因此完整的路由路径为 /system/testindex。
要访问getTestIndex 方法,您需要在浏览器中输入以下URL:
http://localhost:3000/system/testindex
关键点:
通过这种方式,您可以轻松地在NestJS中创建和访问嵌套路由,从而构建更清晰、更易于维护的应用程序。
以上就是Nest.js中嵌套路由:浏览器如何访问?的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号