Skip to content

Commit 172cf2a

Browse files
committed
feat: configured nest config module
1 parent 3068fcc commit 172cf2a

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/app.module.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { Module } from '@nestjs/common';
2+
import { ConfigModule } from '@nestjs/config';
3+
import config from './config';
24

35
@Module({
4-
imports: [],
6+
imports: [
7+
ConfigModule.forRoot({ isGlobal: true, load: [config] }),
8+
],
59
})
610
export class AppModule {}

src/config.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type Config = {
2+
port?: number;
3+
}
4+
5+
// Define environment variables
6+
export default () => ({
7+
port: process.env.PORT || 3000,
8+
} as Config);

src/main.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ConfigService } from '@nestjs/config';
12
import { NestFactory } from '@nestjs/core';
23
import {
34
FastifyAdapter,
@@ -11,7 +12,11 @@ async function bootstrap() {
1112
new FastifyAdapter(),
1213
);
1314

14-
await app.listen(3000, '0.0.0.0');
15+
// Getting configuration from environment
16+
const configService = app.get(ConfigService);
17+
const port = configService.get('port');
18+
19+
await app.listen(port, '0.0.0.0');
1520
}
1621

1722
bootstrap();

0 commit comments

Comments
 (0)