Node.js - (4) 비밀 정보 관리하기

Apeachicetea·2021년 12월 1일
0

Node.js

목록 보기
3/5

비밀정보 옮기기

1.config폴더를 생성하고 그 안에 dev.js, key.js, prod.js파일을 생성한다.
2. dev.js는 로컬환경일때, prod.js는 배포 후일때, key.js는 환경변수에 따라 정보를 어떻게 처리할지 정의해준다.

  • dev.js
module.exports = {
  mongoURI: "mongodb+srv://eodbszla:<password>@cluster0.7lq41.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"
}
  • prod.js
module.exports = {
  mongoURI: process.env.MONGO_URI
                    //heroku에서 설정한 키명이랑 동일해야 한다.
}
  • key.js
if(process.env.NODE_ENV === 'production') {
  //배포 후
  module.exports = require('./prod')
}
else {
 //로컬환경
  module.exports = require('./dev')
}

3.index.js의 비밀정보부분을 변수처리하기

  • index.js
const config = require('./config/key');

mongoose.connect(config.mongoURI)
  .then(()=>console.log('MongoDB Connected...'))
  .catch((err)=>console.log(err));


  1. dev.js를 .ignore에 명시하여 github에 올라가지 않게 하기
  • .gitignore
node_modules

dev.js
profile
웹 프론트엔드 개발자

1개의 댓글

Finding an online casino that fits perfectly with Canadian players can be challenging, but Power Play has made it much simpler. This site dedicates itself to listing and reviewing casinos that are well-suited for the Canadian audience. Last month, when I was looking to enjoy some casino games on a lazy Sunday, this site provided me with a great selection, making my choice easy and the day enjoyable.

답글 달기