• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    해결됨

postman 관련 에러해결방법

22.02.23 03:53 작성 조회수 535

1

저 같은 경우는 별다른 이상이 없는데 axios통신에러가 났었습니다.

저는 vscode에서 작업 중인데 우선 ctrl+c를 해서 서버를 끈 다음 저장을 다시 해주세요. 혹은 vscode 나갔다가 오셔야합니다. 그리고 npm run start를 했을 때 바로 동작이 되는 경우가 있습니다.

이래도 안되면 syntax에러일 확률이 높습니다. 

json타입으로 보낼 때 user.js에도 데이터 타입 다시확인 하시고요.

(vscode의 마켓플레이스에 오탈자나 들여쓰기를 해결해주는 것들이 있습니다.)

 

콘솔로 DB연결과 서버연결이 되는지 순차적으로 확인 하시면서 따라가면 될겁니다

 

+axios 부분 post할 때 경로 확인하세요.

const express = require('express')
const app = express()
const port = 5000
const bodyParser = require('body-parser');
const { User} = require("./models/User");

//application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true}));

//application/json
app.use(bodyParser.json());

//mongodb 연결
const mongoose = require('mongoose')
mongoose.connect('mongodb+srv://id:password@boilerplate.apclh.mongodb.net/myFirstDatabase?retryWrites=true&w=majority')
.then(() => console.log('MongoDB Connected'))
.catch(err => console.log('error'))




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})
            console.log(userInfo.body);
            return res.status(200).json({
                success: true
            })
        })
})

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

 

답변 2

·

답변을 작성해보세요.

2

juju님의 프로필

juju

2022.02.23

큰문제는 없겠지만 mongoDB의 username과 password 노출되어있으세요..!

 

인표님의 프로필

인표

질문자

2022.02.23

 앗 감사합니다 실수로 노출이 되었네요 ㅠㅠ

1

좋은 내용 공유해주셔서 감사합니다 !!