Complete the code to import ConfigModule globally in a NestJS module.
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; @Module({ imports: [ConfigModule.[1]({ isGlobal: true })], }) export class AppModule {}
The ConfigModule.forRoot() method is used to set up the configuration module globally in NestJS.
Complete the code to load a custom configuration file using ConfigModule.
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import customConfig from './custom.config'; @Module({ imports: [ConfigModule.forRoot({ [1]: [customConfig] })], }) export class AppModule {}
The load option allows you to provide custom configuration files or functions to the ConfigModule.
Fix the error in the code to correctly inject ConfigService in a service constructor.
import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; @Injectable() export class MyService { constructor(private readonly [1]: ConfigService) {} }
The parameter name in the constructor should be camelCase, typically configService, to follow NestJS conventions and avoid confusion.
Fill both blanks to create a configuration object with environment variables and default values.
export default () => ({
port: parseInt(process.env.[1] || '[2]', 10),
});The environment variable PORT is commonly used for the server port, and 3000 is a typical default port number.
Fill all three blanks to access a nested configuration value safely using ConfigService.
const dbHost = this.configService.get<string>('database.[1].[2]', [3]);
The nested keys connection and host access the database host, and 'localhost' provides a fallback if the key is missing.