본문 바로가기

WEB

Node.js로 웹서비스 만들기 5일차

몽고DB 연결

https://www.mongodb.com/cloud

 

MongoDB Cloud

MongoDB Cloud is a unified data platform for modern applications and includes a global cloud database, search, data lake, mobile, and application services.

www.mongodb.com

우선 이곳에서 계정을 생성하고 DB를 만들어야 하는데

기억해야 할 부분은

Database User의 아이디와 패스워드 이다.

 

데이터베이스 생성 이후 Connect - Connect your application 선택하면 이런 URL을 얻을 수 있다.

mongodb+srv://아이디:패스워드@cluster0.wg0xpz1.mongodb.net/?retryWrites=true&w=majority

 

그 다음

//server.js

const MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb+srv://아이디:패스워드@cluster0.wg0xpz1.mongodb.net/?retryWrites=true&w=majority', function(error, client){

    app.listen(8080, function(){
        console.log('listening on 8080');
    });

});

서버 실행하고 콘솔에 listening on 3000 찍히면 정상 연결된것임