Complete the code to import the ConfigModule with a custom configuration file.
import { ConfigModule } from '@nestjs/config'; @Module({ imports: [ConfigModule.forRoot({ load: [[1]], })], }) export class AppModule {}
You need to pass a function or object that returns your custom configuration. Here, customConfig is the correct import representing your config file.
Complete the code to create a custom configuration file exporting a function.
export default function [1]() { return { port: parseInt(process.env.PORT, 10) || 3000, }; }
The custom configuration file should export a default function, here named customConfig, that returns the configuration object.
Fix the error in the code to inject the ConfigService and get the custom port value.
import { ConfigService } from '@nestjs/config'; @Injectable() export class AppService { constructor(private readonly configService: [1]) {} getPort(): number { return this.configService.get<number>('port'); } }
You must inject ConfigService to access configuration values inside your service.
Fill both blanks to create a custom configuration file that reads a database URL and exports it.
export default function [1]() { return { databaseUrl: process.env.[2] || 'mongodb://localhost:27017', }; }
The function name databaseConfig is descriptive for database settings. The environment variable DATABASE_URL is a common standard for database connection strings.
Fill all three blanks to use the custom configuration in a service and get the database URL.
import { Injectable } from '@nestjs/common'; import { [1] } from '@nestjs/config'; @Injectable() export class DatabaseService { constructor(private readonly [2]: [1]) {} getDatabaseUrl(): string { return this.[2].get<string>('databaseUrl'); } }
You import and inject ConfigService to access configuration values. The injected variable is commonly named configService.