-
BodyParser & PostMan & 회원 가입 기능Study/Node, React 기초 강의_John Ahn 2023. 2. 13. 23:24
BodyParser
Client에서 보내주는 자료들을 받는 역할을 한다.
BodyParser 설치
아래의 코드를 Visual Studio Code콘솔에 입력한다.
npm install body-parser --save
< 결과 >
PostMan 설치
아래의 사이트에서 다운로드를 진행한다.
https://www.postman.com/downloads/
Download Postman | Get Started for Free
Try Postman for free! Join 20 million developers who rely on Postman, the collaboration platform for API development. Create better APIs—faster.
www.postman.com
< 설치 완료 >
index.js
index.js에 회원가입을 위한 코드를 추가한다.
// express 모듈을 추가 const express = require('express') // 새로운 express 모듈을 생성 const app = express() // port 설정 const port = 5000 // User 모델을 가져옴 const { User } = require("./models/User"); // bodyPaser를 가져옴 const bodyPaser = require("body-parser"); // bodyPaser 옵션주기 // application/s-www-form-urlencoded app.use(bodyPaser.urlencoded({ extended: true })); // application/json app.use(bodyPaser.json()); // 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!') }) app.post('/', (req, res) => { // 회원 가입 할때 필요한 정보들을 client에서 가져오면 그것들을 데이터 베이스에 넣어준다. const user = new User(req.body) user.save((err, userInfo) => { if (err) return res.json({ success: false, err }) return res.status(200).json({ success: true }) }) }) // 5000번 port에서 실행 app.listen(port, () => { console.log(`Example app listening on port ${port}`) })
PostMan에서 데이터 입력 후 Send로 전달
< 코드 >
{ "name" : "d-_-s", "email" : "d-_-s@naver.com", "password" : "1234567" }
< 결과 >
'Study > Node, React 기초 강의_John Ahn' 카테고리의 다른 글
비밀 설정 정보 관리 (0) 2023.02.19 Nodemon (0) 2023.02.14 SSH를 이용하여 GITHUB 연결 (0) 2023.02.13 Git과 Visual Studio 연동 (0) 2023.02.13 Model, Schema 설정 (0) 2023.02.12