Skip to content

Instantly share code, notes, and snippets.

@albizan albizan/config.module.ts
Last active Apr 3, 2019

What would you like to do?
How to Inject configService in main.ts ?
import { Module } from '@nestjs/common';
import { ConfigService } from './config.service';
providers: [
provide: ConfigService,
useValue: new ConfigService(`${process.env.NODE_ENV}.env`),
exports: [ConfigService],
export class ConfigModule {}
import * as dotenv from 'dotenv';
import * as fs from 'fs';
export class ConfigService {
private readonly envConfig: { [key: string]: string };
constructor(filePath: string) {
this.envConfig = dotenv.parse(fs.readFileSync(filePath));
get(key: string): string {
return this.envConfig[key];
getDbType() {
return ('postgres' as 'postgres');
import * as dotenv from 'dotenv';
import { NestFactory } from '@nestjs/core';
import { Logger, ValidationPipe } from '@nestjs/common';
import { AppModule } from './app.module';
import {ConfigService} from './config/config.service';
import * as helmet from 'helmet';
async function bootstrap() {
const config: ConfigService = new ConfigService(`${process.env.NODE_ENV}.env`);
const app = await NestFactory.create(AppModule);
const HTTP_PORT = config.get('HTTP_PORT');
// Use Middleware
/* Use Validation on all endpoints with a global validation Pipe */
app.useGlobalPipes(new ValidationPipe());
await app.listen(HTTP_PORT);
Logger.log(`App running on port ${HTTP_PORT}`, 'Application Bootstrap');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.