Today I Learn/MongoDB

[MongoDB]노드 리액트 몽고디비 연결하기

단추언니 2021. 10. 18. 21:29
반응형

https://cloud.mongodb.com/

 

MongoDB Cloud

MongoDB Cloud는 최신 애플리케이션을 위한 통합 데이터 플랫폼으로, 글로벌 클라우드 데이터베이스, 검색, 데이터레이크, 모바일 및 애플리케이션 서비스를 포함하고 있습니다.

www.mongodb.com

몽고디비 접속! 

 

싱가폴로 설정해주고 Cluster Name 맘대로 바꿔주고 Create Cluster 고고 3분정도 기다리면 클러스터가 만들어진다.

Choose aconnection method 클릭 후 Connect your application 클릭해서 넘어가기

가린부분 복사하기! 

 

vscode로 돌아가 mongoose 설치해준다

$ npm install mongoose --save 
// index.js
const express = require('express')
const app = express()
const port = 5000

const mongoose = require('mongoose')
mongoose.connect('URL')
.then(() => console.log('MongoDB Connected...'))
.catch((e) => console.log('MongoDB error:', e))

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

URL 부분에 복사한 코드 붙혀넣기 

// package.json
{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "mongoose": "^6.0.11"
  }
}

package.json에 node를 실행하기 위한 코드 삽입 "start": "node index.js"

 

실행결과

`

반응형

'Today I Learn > MongoDB' 카테고리의 다른 글

[MongoDB] Model & Schema 몽고디비 모델과 스키마  (0) 2021.10.18