이번 포스팅의 목표는 MongoDB를 연결하고, git 설치 후 github까지 연결하는 것이다.
index.js 파일에 아래의 코드를 추가해주었습니다.
해당 강의에서는 useNewUrlParser: true, usefindandmodify: false을 오류를 막기 위해 입력하라고 하여 입력했는데 오류가 발생했습니다.
알고보니 mongoose 6버전 이상에선 더이상 useNewUrlParser, useUnifiedTopology, useFindAndModify, useCreateIndex을 지원하지 않기 때문에 코드에서 해당 부분만 삭제하면 된다는 것을 알게 되었고,
그 부분을 지우니 정상적으로 코드가 실행되어 MongoDB 연결은 무사히 마칠 수 있었습니다.
const mongoose = require('mongoose')
mongoose.connect('mongodb+srv://angela-sjy:<password>@angela-project.cubvlzt.mongodb.net/?retryWrites=true&w=majority', {
}).then(() => console.log('MongoDB Connected...'))
.catch(err => console.log(err))
아래의 사진은 정상적으로 출력되었음을 보여주는 이미지입니다.
유저의 정보를 유저 데이터베이스에 보관하기 위해 유저 모델과 스키마를 만들기로 하였습니다.
모델은 스키마를 감싸주는 역할을 하고, 스키마는 유저 정보의 틀(type, 길이제한 등)을 제공합니다.
models이라는 폴더를 만들고, 그 안에 User.js 파일을 만들어주었습니다.
const mongoose = require("mongoose");
const userSchema = mongoose.Schema({
name: {
type: String,
maxlength: 50
},
email: {
type: String,
trim: true, //입력에서 빈칸이 있을 시에 그걸 없애준다.
unique: 1
},
lastname: {
type: String,
maxlength: 50
},
role: {
type: Number,
default: 0
},
image: String,
token:{
type: String
},
tokenExp: {
type: Number
}
})
const User = mongoose.model('User',userSchema)
module.exports = {User}
git은 tool / github는 git을 사용하는 일종의 cloud 서비스
git, github를 통해 내가 짠 코드를 사람들과 공유하면서 같이 짤 수 있는데,
여기서 내가 짠 코드들을 안전하게 통신하면서 github에 저장하기 위해 SSH를 이용합니다.