In NestJS, you can retrieve configuration information within controller routes through multiple methods. Below are some of the most common and effective approaches:
1. Using ConfigService
NestJS includes an official configuration package @nestjs/config, which is based on the dotenv library and allows you to easily access environment variables. First, install the package and import ConfigModule:
bashnpm install @nestjs/config
Then, import ConfigModule into your module:
typescriptimport { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; @Module({ imports: [ConfigModule.forRoot()], }) export class AppModule {}
Within your controller, you can retrieve configuration via dependency injection of ConfigService:
typescriptimport { Controller, Get } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; @Controller('example') export class ExampleController { constructor(private configService: ConfigService) {} @Get('config') getConfig(): string { const someValue = this.configService.get<string>('SOME_CONFIG'); return someValue; } }
2. Using @Configurable() Decorator
NestJS allows you to dynamically inject configuration values into method parameters. By using the @Configurable() decorator, you can directly inject configuration values into method parameters.
3. Custom Decorator
You can create a custom decorator to inject configuration values, making the code cleaner and more maintainable:
typescriptimport { createParamDecorator, ExecutionContext } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; export const ConfigValue = createParamDecorator( (data: string, ctx: ExecutionContext) => { const configService = ctx.switchToHttp().getRequest().app.get(ConfigService); return configService.get(data); }, ); @Controller('example') export class ExampleController { @Get('config') getConfig(@ConfigValue('SOME_CONFIG') someValue: string): string { return someValue; } }
4. Direct Environment Variable Injection
Another less recommended approach is to directly use process.env within the controller to retrieve environment variables. This method is less flexible and harder to test:
typescript@Controller('example') export class ExampleController { @Get('config') getConfig(): string { const someValue = process.env.SOME_CONFIG; return someValue; } }
Practical Example:
Suppose you want to retrieve database connection information. You can set the corresponding environment variables in the .env file:
shellDATABASE_HOST=localhost DATABASE_PORT=5432 DATABASE_USER=username DATABASE_PASSWORD=password
Then, within your controller, use ConfigService to retrieve these configuration values:
typescript@Controller('database') export class DatabaseController { constructor(private configService: ConfigService) {} @Get('info') getDatabaseInfo(): any { const host = this.configService.get<string>('DATABASE_HOST'); const port = this.configService.get<number>('DATABASE_PORT'); const user = this.configService.get<string>('DATABASE_USER'); const password = this.configService.get<string>('DATABASE_PASSWORD'); // In actual applications, for security reasons, you may not directly return password information. return { host, port, user }; } }
Through this approach, you can securely and efficiently retrieve configuration information within NestJS controller routes, and easily switch between different environments (development, testing, production) using environment variables.