-
Mongo DB 연결Study/Node, React 기초 강의_John Ahn 2023. 2. 11. 22:57
Visual Studio와 Mongo DB 연결
아래의 Mongo DB 사이트에서 회원가입을 진행한다.
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
클러스터(Data Base) 생성
데이터 베이스를 생성한다.
무료 버전으로 생성한다.
본인에게 맞는 환경을 설정한다.
DB에 접속할 ID, PassWord를 생성한다.
DB에 연결하기 위해 Connect를 선택한다.
애플리케이션 연결을 선택한다.
Mongoose 설치
npm install mongoose --save
< 결과 >
package.json에 mongoose가 자동 추가된다.
{ "name": "boilerplate", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "node index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "d-_-s", "license": "ISC", "dependencies": { "express": "^4.18.2", "mongoose": "^6.9.1" } }
Visual Studio와 Mongoo DB 연결
애플리케이션 코드에 문자열을 추가해 준다.
아이디와 비밀번호 부분은 본인의 아이디 비밀번호를 사용한다.
// express 모듈을 추가 const express = require('express') // 새로운 express 모듈을 생성 const app = express() // port 설정 const port = 5000 // mongoose 연결 const mongoose = require('mongoose') // error 발생을 막기 위한 코드 mongoose.connect("mongodb+srv://<아이디>:<비밀번호>@boilerplate.6t2ootw.mongodb.net/?retryWrites=true&w=majority" ).then(() => console.log('MongoDB Connected...')) // DB 연결 확인을 위한 코드 .catch(err => console.log(err)) // Hello World! 전송 app.get('/', (req, res) => { res.send('Hello World!') }) // 5000번 port에서 실행 app.listen(port, () => { console.log(`Example app listening on port ${port}`) })
< 결과 >
'Study > Node, React 기초 강의_John Ahn' 카테고리의 다른 글
BodyParser & PostMan & 회원 가입 기능 (0) 2023.02.13 SSH를 이용하여 GITHUB 연결 (0) 2023.02.13 Git과 Visual Studio 연동 (0) 2023.02.13 Model, Schema 설정 (0) 2023.02.12 개발 환경 설치_Node.js, express js, Visual Studio (Window) (0) 2023.02.11