mongoose 설치 및 사용하기

woody kim·2021년 6월 20일
0

MongoDB

목록 보기
4/6
post-thumbnail

mongoose는 Node.js 환경에서 사용하는 MongoDB 기반 ODM(Object Data Modelling) 라이브러리이다. 이 라이브러리는 데이터베이스 문서들을 자바스크립트 객체처럼 사용할 수 있게 도와준다.

mongoose 설치

$ yarn add mogoose

mongoose로 서버와 데이터베이스 연결

mongoose의 connect 함수를 사용하여 서버와 데이터베이스를 연결할 수 있다.

const express = require('express')
const app = express()
const port = 3000
const MONGO_URI = 'mongodb://localhost:27017/test_db';

const mongoose = require('mongoose');

mongoose.connect(MONGO_URI, { 
  useNewUrlParser: true, 
  useFindAndModify: false 
}).then(() => {
  console.log('Connected to MongoDB')
}).catch(e => {console.log(e);})

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

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

코드를 실행하고 다음과 같은 문구가 출력되면 데이터베이스에 성공적으로 연결이 된 것 이다.

참고

리액트를 다루는 기술

profile
안녕하세요

0개의 댓글