커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
API (nodejs) 질문입니다!!
undefined주차
북마크
강*현
댓글
10
추천
0
조회수
20
조회수
20
답변 완료

안녕하세용 API 질문입니다!!

웹 개발 종합반에서 배운 내용을 토대로 Nodejs 를 활용한 서버 개발을 하고 있는데 모르는게 생겨 질문드립니다~~


1번 코드/ 사진 : 웹개발 종합반 python 과정

2번 코드/ 사진 : Nodejs 과정


질문 : 아래 1번 코드의 post 부분이 api 화면을 구성해주는 게 맞나요? 제 코드에서도 API를 생성하고 싶은데 req.body를 전달해도 2번 사진처럼 내용이 전달이 되지 않습니다. api 부분만 잘 나온다면 그 이후 DB 작업과 화면 재구성까지 할 수 있을 것 같아 부탁드립니다!



1번 코드
@app.route("/homework", methods=["POST"])
def homework_post():
    name_receive = request.form['name_give']
    comment_receive = request.form['comment_give']

    doc = {
        'name' : name_receive,
        'comment': comment_receive,
    }
    db.iu.insert_one(doc)

    return jsonify({'msg':'POST 연결 완료!'})

@app.route("/homework", methods=["GET"])
def homework_get():

    orders_list = list(db.iu.find({},{'_id':False}))

    return jsonify({'msg':orders_list})

if __name__ == '__main__':
    app.run('0.0.0.0', port=5000, debug=True)


1-1번 코드
function save_comment(){
    let name1 = $('#name').val()
    let comment1 = $('#comment').val()

    $.ajax({
        type: 'POST',
        url: '/homework',
        data: {name_give: name1 , comment_give: comment1},
        success: function (response) {
            alert(response['msg'])
            window.location.reload()
        }
    })
}

1번 사진

스파르타 즉문즉답

----------------------------------------------------------------------------------------------------------------------------------------

2번 코드
const express = require('express')
var app = express();

app.get('/bakery925',(req,res)=>{
    res.send(req.body);
})
app.post('/bakery925',(req,res)=>{
    console.log(req.body); 
})


2-2번 코드
    const req = {
        name : 'name',
        price : 'price',
        count : 'count',
        allPrice: parseInt(totalPrice),
    }

    fetch('/bakery925',{
        method:'POST',
        headers: {
            'Content-Type':'application/json',
        },
        body:JSON.stringify(req), 
    })


2번 사진

스파르타 즉문즉답


취소
 공유
취소
댓글 0
댓글 알림
나의얼굴