पाठ 20 / 33

Environment और Configuration

Environment variables और per-environment configuration को manage करें।

ConfigModule setup

.env files को automatically load और validate करने के लिए @nestjs/config का उपयोग करें।

import { ConfigModule } from '@nestjs/config';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: '.env'
    })
  ]
})
export class AppModule {}

Config को access और validate करें

Values पढ़ने के लिए ConfigService को inject करें; schema validation के लिए Joi का उपयोग करें।

import { ConfigService } from '@nestjs/config';

@Injectable()
export class AppService {
  constructor(private config: ConfigService) {}

  getPort() {
    return this.config.get<number>('PORT') || 3000;
  }
}