AWS NestJS Starter 项目教程
1. 项目的目录结构及介绍
aws-nestjs-starter/
├── src/
│ ├── app.module.ts
│ ├── main.ts
│ ├── config/
│ │ ├── configuration.ts
│ │ └── environment.ts
│ ├── modules/
│ │ ├── example/
│ │ │ ├── example.module.ts
│ │ │ ├── example.controller.ts
│ │ │ └── example.service.ts
│ │ └── ...
│ └── ...
├── test/
│ └── app.e2e-spec.ts
├── package.json
├── tsconfig.json
└── ...
src/
:项目的主要源代码目录。app.module.ts
:应用程序的根模块。main.ts
:应用程序的入口文件。config/
:配置文件目录。configuration.ts
:配置文件的加载和设置。environment.ts
:环境变量的处理。
modules/
:模块目录,包含各个功能模块。example/
:示例模块。example.module.ts
:示例模块的定义。example.controller.ts
:示例模块的控制器。example.service.ts
:示例模块的服务。
test/
:测试文件目录。app.e2e-spec.ts
:端到端测试文件。
package.json
:项目的依赖和脚本配置。tsconfig.json
:TypeScript 编译配置。
2. 项目的启动文件介绍
main.ts
是项目的启动文件,负责引导应用程序的启动。以下是 main.ts
的主要内容:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
NestFactory.create(AppModule)
:创建 NestJS 应用程序实例。app.listen(3000)
:监听 3000 端口,启动应用程序。
3. 项目的配置文件介绍
config/
目录包含项目的配置文件,主要负责加载和设置配置。
configuration.ts
import { readFileSync } from 'fs';
import * as yaml from 'js-yaml';
import { join } from 'path';
const YAML_CONFIG_FILENAME = 'config.yaml';
export default () => {
return yaml.load(
readFileSync(join(__dirname, YAML_CONFIG_FILENAME), 'utf8'),
) as Record<string, any>;
};
- 读取
config.yaml
文件并解析为 JavaScript 对象。
environment.ts
export const environment = {
production: false,
database: {
host: 'localhost',
port: 5432,
},
};
- 定义环境变量,如数据库配置等。
以上是 AWS NestJS Starter 项目的基本教程,涵盖了项目的目录结构、启动文件和配置文件的介绍。