创建项目完成后,项目中的src目录结构比较单一,需要做出调整。
初始目录
arduino
-|src
--| app.controller.spec.ts // 控制器的单元测试(目前先忽略)。
--| app.controller.ts // 一个具有单一路由的基础控制器
--| app.module.ts // 应用的根模块
--| app.service.ts // 一个具有单一方法的基础服务
--| main.ts // 应用的入口文件,使用核心函数 NestFactory 创建 Nest 应用实例。
当前目录
将目录结构作出以下划分:
diff
-| src
--| main.ts
--| app.module.ts
--| module // 细分每个模块
---| test
----| test.controller.spec.ts
----| test.controller.ts
----| test.module.ts
----| test.serviec.ts
--| config // 配置文件等
--| xxx
具体代码
如下为项目划分的具体代码:
main.ts
javascript
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
app.module.ts
typescript
import { Module } from '@nestjs/common';
import { TestModule } from './module/test/test.module';
@Module({
// 将模块引入
imports: [TestModule],
})
export class AppModule {}
test目录代码
typescript
// test.controller.ts
import { Controller, Get } from '@nestjs/common';
import { TestService } from './test.service';
@Controller()
export class TestController {
constructor(private readonly TestService: TestService) {}
@Get()
getHello(): string {
return this.TestService.getHello();
}
}
typescript
// test.module.ts
import { Module } from '@nestjs/common';
import { TestController } from './test.controller';
import { TestService } from './test.service';
@Module({
controllers: [TestController],
providers: [TestService],
})
export class TestModule {}
kotlin
// test.service.ts
import { Injectable } from '@nestjs/common';
@Injectable()
export class TestService {
getHello(): string {
return 'Hello World!';
}
}