環境變量配置簡述
程序在不同的環境下需要不同的環境變量,例如生產環境、測試環境以及開發環境所需要不同的數據庫信息:鏈接地址、鏈接端口號、登錄用戶名和密碼相關信息。為了解決這個問題需要進行相關操作。
在 Nest 中最佳方案創建一個 ConfigModule,該 ConfigModule 公開一個 ConfigService ,在 ConfigService 加載特有環境的 .env 文件。 Nest 提供了 @nestjs/config 開箱即用的依賴包。
配置
npm 生態有很多相關的依賴包,比如最簡單的:
1
2
|
yarn add dotenv-flow yarn add @types /dotenv-flow -D |
安裝好了直接在 main.ts 使用:
1
2
3
4
5
6
7
|
import * as dotenv from 'dotenv-flow' /** * 導入 .env 環境 * https://www.npmjs.com/package/dotenv-flow */ dotenv.config() |
就可以使用對應的環境 .env 變量了,不過這樣使用官方推薦軟件包:@nestjs/config :
1
|
yarn add @nestjs /config |
在 app.module.ts 中的 forRoot 靜態方法配置環境變量 .env 解析:
1
2
3
4
5
6
7
|
import { Module } from '@nestjs/common' import { ConfigModule } from '@nestjs/config' @Module({ imports: [ConfigModule.forRoot()] }) export class AppModule {} |
然后在項目根目錄下新建 .env 文件:
1
2
3
4
5
|
DATABASE_USER= DATABASE_PASSWORD= DATABASE_NAME= DATABASE_PORT= DATABASE_HOST= |
自定義 env 路徑
如果 .env 需要細化生產、測試和開發環境可以按照下面進行配置:
1
2
3
|
ConfigModule.forRoot({ envFilePath: [ '.env.development.local' , '.env.development' ], }) |
其中排序越前面則優先級最高,但在啟動命令中設置環境變量則是最高,例如:
1
|
export DATABASE_USER=root && nest start |
自定義配置文件
對于復雜的項目,需要把用到的可配置變量需要收集起來,比如新建 src/config/configuration.ts :
1
2
3
4
5
6
7
|
export default () => ({ port: parseInt(process.env.PORT, 10) || 3000, database: { host: process.env.DATABASE_HOST || 'localhost' , port: parseInt(process.env.DATABASE_PORT, 10) || 3306 } }) |
然后在 ConfigModule.forRoot 加載:
1
2
3
4
5
6
7
8
9
10
|
import configuration from './config/configuration' @Module({ imports: [ ConfigModule.forRoot({ load: [configuration] }) ] }) export class AppModule {} |
讀取配置變量
如果需要讀取相關的配置變量需要用到 ConfigService ,需要在用到的 *.module.ts 文件引入:
1
2
3
4
|
@Module({ imports: [ConfigModule], // ... }) |
如果涉及的很多地方要寫,每個 module 都要引入很煩人,可以在上面的 app.module.ts
添加一個字段:
1
2
3
4
5
6
7
8
9
10
11
|
import configuration from './config/configuration' @Module({ imports: [ ConfigModule.forRoot({ isGlobal: true , load: [configuration] }) ] }) export class AppModule {} |
然后在構造函數注入使用:
1
2
3
|
import { ConfigService } from '@nestjs/config' constructor(private configService: ConfigService) {} |
獲取配置變量例如:
1
2
|
const dbUser = this .configService.get<string>( 'DATABASE_USER' ) const dbHost = this .configService.get<string>( 'database.host' ) |
序列化
序列化指的是程序在網絡響應中返回對象發送之前的過程,將提供的信息要進行轉換和清理才能發給客戶端:比如查詢某個用戶,一般來說可以返回當前用戶實體信息,但里面的密碼信息是不可以發送給客戶端的,所以這邊要做一些轉換。
還好 Nest 提供一個 class-transformer 相當好用的軟件包:
1
|
yarn add class-transformer |
比如在下列的用戶實體信息排除密碼信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import { Exclude } from 'class-transformer' export class UserEntity { id: number firstName: string; lastName: string; @Exclude() password: string; constructor(partial: Partial<UserEntity>) { Object.assign( this , partial); } } |
然后在控制器處理查詢用戶方法:
1
2
3
4
5
|
@UseInterceptors(ClassSerializerInterceptor) @Get( ':id' ) findOne(@Param( 'id' ) id: string): Promise<UserEntity> { return this .userService.findOne(id) } |
最終查詢會忽略密碼顯示。
總結
到此這篇關于Nest.js環境變量配置與序列化的文章就介紹到這了,更多相關Nest.js環境變量配置內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://iiong.com/nest-js-environment-variable-configuration-and-serialization/