본문 바로가기

CodeTech/Node.js

MongoDB - [express js app과 연결하기]

MongoDB

- 데이터베이스 서비스이다. 

www.mongodb.com/

 

The most popular database for modern apps

We're the creators of MongoDB, the most popular database for modern apps, and MongoDB Atlas, the global cloud database on AWS, Azure, and GCP. Easily organize, use, and enrich data — in real time, anywhere.

www.mongodb.com

- 오른쪽 상단의 Try Free 를 선택해서 회원가입을 해준다.

 

하다보면 cluster라는 것을 만들어야 하는데

위 페이지를 보면 cloud 상에서 mongoDB를 구축하기 위해서 해당 옵션들을 고르는 과정이다.

가장 가까운 지역인 싱가폴을 선택한다.

 

Cluster Tier는 M0 SandBox를 선택하고 

ClusterName을 선택한 후에 CreateCluster 버튼을 클릭하면

이렇게 클러스터가 생성되는 과정이 뜬다 이과정은 약 몇분 걸리니 천천히 기다리도록 합니다.

생성이 되면 connect 버튼을 클릭해서 mongoDB userID와 password를 생성합니다.

- 나중에 express js app과 연결 할 때 필요합니다.

- 생성하고 나면 choose a connection method를 클릭하고

- Connect your application을 클릭하면

위 화면이 나오는데

 

mongodb+srv://dbDoodream:<password>@boilerplate.roags.mongodb.net/<dbname>?retryWrites=true&w=majority

위 코드를 복사해서 아무곳에 붙여 넣어 저장해놓자.

 

DB에 접근가능한 IP추가

 

Network Access 부분을 들어가면 해당 디비에 접근 가능한 ip address를 추가하는 기능이 있는데, 

오른쪽에 Add IP Address를 클릭하고 allow anywhere를 클릭해서 어디서든 db에 접근 가능하도록 설정한다. 

 

 

Mongoose 

- mongoDB를 간편하게 사용할수 있는 ObjectModelingTool이다.

- 해당 툴은 라이브러리 이므로 express js app의 터미널에 yarn add mongoose 로 다운로드를 받자.

yarn add mongoose

 

MongoDB와 Express js app 연결

- mongoose 라이브러리를 가져오고

- mongoose.connect('위에 복사해놨던 부분을 붙여넣기 한후 <password> 부분을 mongoDB 비번을 생성했던 것으로 바꿔준다.', {

  이 안에 들어있는 문구들은 아래 사이트를 확인해보자

}).then(()=> console.log('이기능은 연결이 잘되었을 경우 출력하는 메세지이고').catch(err => console.log(err // 이부분은 연결이 안되었을 경우 에러메세지를 출력하는 경우이다.))

mongoosejs.com/docs/deprecations.html

 

Mongoose v5.11.10: Deprecation Warnings

Deprecation Warnings There are several deprecations in the MongoDB Node.js driver that Mongoose users should be aware of. Mongoose provides options to work around these deprecation warnings, but you need to test whether these options cause any problems for

mongoosejs.com

- 위 사이트는 몽구스 다큐먼트인데 여러워닝 데이터가 뜨게 될때 안뜨게 하는 방법에 대해서 설명하고 있다.

설명을 읽다보면 use...와 같은 옵션들을 설정함으로서 warning 메세지를 띄우지 않을수 있다. 물론 해당 메세지들은 아무런 동작에 이상이 없다.

이제 다시 express js app을 실행하면

 

 

'CodeTech > Node.js' 카테고리의 다른 글

Node.js - 1 [설치 (express)]  (0) 2021.01.07