-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
documentation문서화 📄문서화 📄
Description
Configuration
ConfigModule 설정을 통해 환경변수를 .env파일에 저장하고 사용해보자!
1. 패키지 Install
npm install @nestjs/config2. ConfigModule 설정
import { ConfigModule } from '@nestjs/config';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true, // ConfigModule을 전역 모듈로 설정한다.
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}3. .env 파일작성
# 사용할 환경 변수들을 작성한다.
PORT=3000
DATABASE_URL=mysql://user:password@localhost:3306/mydatabase
JWT_SECRET=secretkey
4. 서비스로 주입받아 사용
이제 configService.get() 메서드로 환경변수를 읽어올 수 있다.
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
@Injectable()
export class AppService {
constructor(private configService: ConfigService) {}
getDatabaseUrl(): string {
return this.configService.get<string>('DATABASE_URL'); // .env 파일에서 DATABASE_URL 값 읽어오기
}
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
documentation문서화 📄문서화 📄