세번째, 회원가입 기능을 만든다. 🌷
npm install body-parser --save
body-parser는 API 요청을 받은 body의 값을 파싱하는 역할을 한다.
req.body로 출력해준다.
const bodyParser = require('body-parser')
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
extended: true
}))
// parse application/json
app.use(bodyParser.json())
app.use(cookieParser())
index.js 파일에 추가한다.
const {User} =require('./models/User')
정보를 보내는 것이므로 POST method를 사용한다.
app.post('/api/user/register',(req,res) => {
// 회원 가입 할때 필요한 정보들을 client 에서 가져오면
//그거를 데이터 베이스에 넣어 준다.
const user = new User(req.body);
user.save((err,userInfo) => { // mongdb command
if(err) return res.json({ success: false,err})
return res.status(200).json({ success: true })
})
})
비밀번호에서 암호화한 기능은 다음 게시글!
암호화하지 않으면
다음과 같이 비밀번호가 그대로 노출된다...!