19.11.06 MONGODB와 MONGOOSE

sykim·2019년 11월 6일
0

MONGODB

SQL과 NoSQL 두 종류의 데이터베이스가 있는데 몽고디비는 NoSQL에 해당한다.
몽고디비 사이트에서 다운을 받고 CMD 창에서

> mongod

이때 아무 반응이 없다면 시스템 변수 편집창에서 내가 설치한 경로 중 몽고디비 bin 경로를 추가해주면 해결이 된다.

MONGODB는 C++ 언어로 자바스크립트가 읽지 못하기 때문에 이를 해석해주는 중간 다리 역할이 필요하다.

npm i mongoose

mongoose 는 nodejs와 mongodb를 연결시켜준다.

profile
블로그 이전했습니다

1개의 댓글

comment-user-thumbnail
2019년 11월 6일

몽고디비를 몽구스를 통해 연결하는 법

db.js

import mongoose from "mongoose";

mongoose.connect(
  "mongodb://localhost:27017/we-tube",
  {
    useNewUrlParser: true,
    useFindAndModify: false
  }
);

const db = mongoose.connection;

const handleOpen = () => console.log("✅  Connected to DB");
const handleError = error => console.log(`❌ Error on DB Connection:${error}`);

db.once("open", handleOpen);
db.on("error", handleError);

몽구스가 연결된 db.js를
작업 파일 중 포트연결 코드가 있는 init.js 파일에 임포트 시켜준다
init.js

import "./db";
import app from "./app";

const PORT=4000
const handleListening = () => console.log(`Listening on : http://localhost:${PORT}`);

app.listen(PORT,handleListening);
답글 달기