<오늘 배운 것>
1. 몽고디비 온라인 클라우드 환경설정
2. .gitignore 꼭 추가하기
3. .env 파일에 패스워드 저장
#몽고디비 환경설정
아래 포스팅을 참고하여 연습용 몽고디비 클라우드를 얻었다.
# .gitignore
.gitignore 파일 생성
node_modules
*.log
# git init 하고 git add . 하기 전에 꼭 .gitignore를 만들고 node_modules 까지 업로드되지 않도록 하자!
# 이미 git add . 해버렸다면
git rm -r --cached . //add 했던 내역 삭제
git add . //gitignore파일 만든 후에 다시 add
https://helloinyong.tistory.com/106
#환경변수 설정
vi ~/.bash_profile
-> 다시 취소함. env파일에 저장하는 걸로 바꿈.
# .env 파일에 중요정보 저장하기
.env는 git 에 업로드되지 않아야할 데이터베이스 패스워드 등을 저장하는 파일이다.
$npm install dotenv
.env , .envcpy 파일 만들기
var express = require("express");
var mongoose = require("mongoose");
var app = express();
require('dotenv').config(); // .env파일에서 환경변수 불러오기
//DB setting
mongoose.set('useNewUrlParser', true);
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);
mongoose.set('useUnifiedTopology', true);
mongoose.connect(process.env.MONGO_URI);
var db = mongoose.connection;
db.once("open", function() {
console.log("DB connected");
});
db.on("error", function(err) {
console.log("DB ERROR: ", err);
});
//Other settings
app.set("view engine", "ejs");
app.use(express.static(__dirname+"/public"));
//port setting
var port = process.env.PORT || 3000; //port값 설정되어 있지 않다면 3000사용.
app.listen(3000, function() {
console.log("server on! http://localhost:"+port);
});
db connected!
'두드리는 개발자 홍차 > Database' 카테고리의 다른 글
TIL #022 AWS S3 버킷 생성 - 삽질의 시작 (1) | 2019.12.05 |
---|---|
TIL #020 이미지 서버 생성기1 (2) | 2019.12.02 |
TIL #014 MySQL 데이터 모델링 (0) | 2019.11.22 |
TIL #004 MongoDB (0) | 2019.11.06 |
댓글