JEoN-Ha / Senior-Project

4학년 졸업작품
MIT License
1 stars 3 forks source link

fetch 200, 400 응답에 관하여 질문이 있습니다. #85

Closed fora22 closed 3 years ago

fora22 commented 3 years ago

통상적으로 HTTPS 응답(?)에서 404 Error 를 많이 본적이 있습니다. 저희 Swagger 를 통해 정상작동하면 200 응답을 에러시 400 응답을 하도록 했는데 이는 router 에서 변수를 send() 한건가요? 아니면 자동적으로 Promise 객체에 응답 숫자가 있는건가요?

https://github.com/JEoN-Ha/Senior-Project/blob/bf1acb1163fe2132c35424a40351cd79e3c4965e/jiyeon/jeonha_redux/public/swagger.yaml#L13-L69

예를 들어 위 api 명세서에 따라

router.post('/signUp', (req, res) => {
    const userwebid = `'${req.body.userWebId}'`;
    const username = `'${req.body.userName}'`;
    const pw = `'${req.body.pw}'`;
    const phonenum = `'${req.body.phoneNum}'`;
    const carid = `'${req.body.carId}'`;

    const sqlCodeToUserTable = `
    insert into usertable(UserWebId, UserName, PW, PhoneNum)
    values (${userwebid}, ${username}, ${pw}, ${carid})    
    `;
    let idOverlap = true;
    let pwOverlap = true;
    let dbError = true;

    db.query(sqlCodeToUserTable, (err, rows) => {
        if(err) {
           idOverlap = false; 
           pwOverlap = false;
        } 
    })

    const sqlCodeToCarTable = `
    insert into car(CarWebId, CarId)
    values (${userwebid}, ${carid})    
    `;

    db.query(sqlCodeToCarTable, (err, rows) => {
        if(err) {
           errorCheck = false; 
        } 
    })

    if (errorCheck) {
        res.send('200');
    } else {
        res.send('400');
    }
})

이렇게 send()로 보내줘야 하나요??

jongfeel commented 3 years ago

이렇게 하면 body의 값 자체가 200, 400이 되기 때문에 statusCode에 값을 넣어서 보내야 합니다.

https://nodejs.org/api/http.html#http_response_statuscode

fora22 commented 3 years ago

이렇게 하면 body의 값 자체가 200, 400이 되기 때문에 statusCode에 값을 넣어서 보내야 합니다.

https://nodejs.org/api/http.html#http_response_statuscode

statusCode 가 있군요. 하면서 계속 뭔가 이상하다 싶었습니다. ㅎㅎㅎㅎ 감사합니다!

fora22 commented 3 years ago

@kiJiyeon 의문 해결!