[Node.js] 회원가입 요청보내기

kwonseokki·2022년 8월 19일
0

node

목록 보기
3/8

postman 을 이용하여 회원가입 요청을 보내고
성공을 확인하는 연습을 해보자

index.js

const express = require("express"); // express 모듈 가져오기
const app = express(); // 새로운 express 앱생성
const port = 5000; // 포트번호지정
const { User } = require("./models/User");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
//application/x-www.form.urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// application/json
app.use(bodyParser.json());
mongoose
  .connect(
    "mongodb+srv://admin:1234@react-node.bgg018w.mongodb.net/?retryWrites=true&w=majority"
  )
  .then(() => {
    console.log("mongoDB connected!");
  })
  .catch((err) => {
    console.log(err);
  });

app.get("/", (req, res) => {
  res.send("Hello World! 노드 실행");
});

app.post("/register", (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,
    });
  });
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

bodyparser

bodyparse 란 요청된 body값에 접근하기 위한 미들웨어 라고 한다
index.js 에서 req.body 요청된 body값을 바로 읽을수 있는것이다.

request


요청완료

profile
프론트개발자가 되고싶어요

0개의 댓글