[nest.js] config 사용법

본문에서는 mysql 을 사용합니다.

관련 package 설치
npm i -g win-node-env( 윈도 환경일때만 )
npm i config --save

프로젝트 root 에 config 폴더 생성
- default.yaml
- development.yaml
- production.yaml

default.yaml( 기본 config 내용 )
server :
    port : 서버포트

db :
    type : 'mysql'
    port : db포트
    database : 'db명'
    
jwt :
    expiresIn : 3600

development.yaml( 개발서버용 )
db :
    host : 'db주소'
    username : 'db유저명'
    password : '비밀번호'
    synchronize : true( 개발서버에서는 entity 변경에 의한 db 동기화 on )

jwt :
    secret : 'token생성시크릿키'
    

production.yaml( 운영서버용 )
db :
    synchronize : false( 운영에서 잘못 동기화 시켰다가는 죽음 )

how to use on source code

main.ts

import config = require( "config" );
.
.
.
async function bootstrap()
{
.
.
.
	await app.listen( config.get( 'server' ).port );
    // config 파일에 있는 port( 3000 ) 을 사용
}

import * as config from 'config' 로 하면 오류가 생기니 조심

Subscribe to X세대 신입사원

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe
774-86-01972 cinnabar.3d@gmail.com