투케이2K

55. (NodeJs) [Mac Os] [Request] : http 요청 req.body 확인 및 json 형태 파싱 데이터 확인 수행 본문

NodeJs

55. (NodeJs) [Mac Os] [Request] : http 요청 req.body 확인 및 json 형태 파싱 데이터 확인 수행

투케이2K 2024. 1. 8. 20:18

[개발 환경 설정]

개발 툴 : VS CODE

개발 언어 :NodeJs

 

[app.js : 소스 코드]

// ----------------------------------------------------------------------------------------------

const express = require('express')
const app = express()

// ---------------------------------------

app.set('view engine', 'ejs') // [Page] : [Render]
app.set('views', './views') // [Page] : [Render]

// ---------------------------------------

var bodyParser = require('body-parser'); // [body-parser 사용]

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

// ----------------------------------------------------------------------------------------------

// [Get] : Path = [/] : http://localhost:3000/
app.get('/', function (req, res) {
    console.log("")
    console.log("==============================================================================")
    console.log("[Server] :: [App] :: [Path = /] :: [Start]")
    console.log("==============================================================================")
    console.log("")

    // [Web Return Response]
    res.send('Hello World !!')
})

// ----------------------------------------------------------------------------------------------

// [Post] : Path = [/login] : http://localhost:3000/login
app.post('/login', function (req, res) {
    console.log("")
    console.log("==============================================================================")
    console.log("[Server] :: [App] :: [Path = /login] :: [Start]")
    console.log("--------------------------------------------------------------------------")
    console.log("[INPUT] :: [HttpVersion] :: " + JSON.stringify(req.httpVersion))
    console.log("--------------------------------------------------------------------------")
    console.log("[INPUT] :: [HostName] :: " + JSON.stringify(req.hostname))
    console.log("--------------------------------------------------------------------------")
    console.log("[INPUT] :: [Path] :: " + JSON.stringify(req.path))
    console.log("--------------------------------------------------------------------------")
    console.log("[INPUT] :: [Protocol] :: " + JSON.stringify(req.protocol))
    console.log("--------------------------------------------------------------------------")
    console.log("[INPUT] :: [Headers] :: " + JSON.stringify(req.headers))
    console.log("--------------------------------------------------------------------------")
    console.log("[INPUT] :: [Accepted] :: " + JSON.stringify(req.accepted))
    console.log("--------------------------------------------------------------------------")
    console.log("[INPUT] :: [Method] :: " + JSON.stringify(req.method))
    console.log("--------------------------------------------------------------------------")
    console.log("[INPUT] :: [Payload] :: " + JSON.stringify(req.payload))
    console.log("--------------------------------------------------------------------------")
    console.log("[INPUT] :: [Cookies] :: " + JSON.stringify(req.cookies))
    console.log("--------------------------------------------------------------------------")
    console.log("[INPUT] :: [Query] :: " + JSON.stringify(req.query))
    console.log("--------------------------------------------------------------------------")
    console.log("[INPUT] :: [Body] :: " + JSON.stringify(req.body))
    console.log("==============================================================================")
    console.log("")

    let { id, pw } = req.body;

    // [Web Return Response]
    if (req.body != undefined && id != undefined && pw != undefined){
        res.status(200).send(JSON.stringify({"result" : id}));
    }
    else {
        res.status(403).send(JSON.stringify({"result" : "error"}));
    }
})

// ----------------------------------------------------------------------------------------------

// [Server] : [Start]
//*
app.listen(3000, function () {
    console.log("")
    console.log("==============================================================================")
    console.log("[Server] :: [Port = 3000] :: [Start]")
    console.log("==============================================================================")
    console.log("")
})
// */

// ----------------------------------------------------------------------------------------------

[결과 출력]

 

 

반응형
Comments