투케이2K

16. (NodeJs) [Mac Os] [EJS] : Node Js 에서 ejs 뷰 (view) 렌더링 및 이중 json 데이터 파싱 , 데이터 view 뷰에 표시 수행 본문

NodeJs

16. (NodeJs) [Mac Os] [EJS] : Node Js 에서 ejs 뷰 (view) 렌더링 및 이중 json 데이터 파싱 , 데이터 view 뷰에 표시 수행

투케이2K 2023. 12. 31. 13:47

[개발 환경 설정]

개발 툴 : 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]

//app.use(express.urlencoded({ extended: true })) // [Http] : [URL] : [Encode]

app.use(express.json()) // [Http] : [Body] : [Json]

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

// [Get] : Path = [/getRender] : http://localhost:3000/getRender?id=admin
app.get('/getRender', function (req, res) {
    console.log("")
    console.log("==============================================================================")
    console.log("[Server] :: [Get] :: [Path = /getRender] :: [Start]")
    console.log("--------------------------------------------------------------------------")
    console.log("[INPUT] :: [Headers] :: " + JSON.stringify(req.headers))
    console.log("--------------------------------------------------------------------------")
    console.log("[INPUT] :: [Cookies] :: " + JSON.stringify(req.cookies))
    console.log("--------------------------------------------------------------------------")
    console.log("[INPUT] :: [Params] :: " + JSON.stringify(req.query))
    console.log("--------------------------------------------------------------------------")
    console.log("[INPUT] :: [Body] :: " + JSON.stringify(req.body))
    console.log("==============================================================================")
    console.log("")

    // [파라미터값 체크]
    if (req.query.id == 'admin'){
        res.render("index", {userInfo : {name: 'twok', age: 30}})
    }
    else {
        res.status(403).json({ "result" : "error" });
    }

})

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

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

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

[index.ejs : 소스 코드]

<!DOCTYPE HTML>
<html lang="ko">
<head>
    <title>WebTest</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    




    <!-- ================================================================================== -->
    <!-- [반응형 구조 만들기] -->
    <!-- ================================================================================== -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"/>
    




    <!-- ================================================================================== -->
    <!-- [자바 스크립트 및 j쿼리 파일] -->
    <!-- ================================================================================== -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <script src="https://code.jquery.com/jquery-latest.min.js"></script> 




    <!-- ================================================================================== -->
    <!-- [내부 CSS 스타일 지정] -->
    <!-- ================================================================================== -->
    <style>


        /* [전체 설정 실시] */
    	* {
            font-family : 'Noto Sans KR' , sans-serif;           
            margin : 0;
            padding : 0;
    	}
    	




        /* [html , body 설정] */
        html, body{			
            width : 100%;          
            height : 100%;
            margin : 0 auto;
            padding : 0;
            border : none;         

            /* [모바일 스크롤 시 부드럽게 처리] */
            overflow:scroll-y; 
            -webkit-overflow-scrolling:touch;
        }
        /* [body 스크롤바 메인 스타일 지정] */
        body::-webkit-scrollbar {           
            width: 20px;                 
            background-color: #c1c1c1;
        }
        /* [body 스크롤바 thumb 스타일 지정] */
        body::-webkit-scrollbar-thumb {                 
            background-color: #444444;
        }
                     
    </style>
    




    <!-- ================================================================================== -->
    <!-- [내부 자바스크립트 J쿼리 이벤트 지정] -->    
    <!-- ================================================================================== -->
    <script>

        /* [dom 생성 및 이벤트 상시 대기 실시] */
        document.addEventListener("DOMContentLoaded", ready);
        function ready(){
            console.log("");
            console.log("=====================================================");
            console.log("[window ready] : [start]"); 
            console.log("=====================================================");           
            console.log("");
        }





        /* [html 최초 로드 및 이벤트 상시 대기 실시] */
        window.onload = function() {
            console.log("");
            console.log("=====================================================");
            console.log("[window onload] : [start]");
            console.log("=====================================================");
            console.log(""); 

        };





        /* [html 화면 사이즈 변경 이벤트 감지] */
        window.onresize = function() {
            console.log("");
            console.log("[window onresize] : [start]");
            console.log(""); 
        };

    </script>
    
</head>





<!-- ================================================================================== -->
<!-- [body 콘텐츠 작성] -->
<!-- ================================================================================== -->
<body>

    <h1>name : <%= userInfo.name %></h1>
    <h1>age : <%= userInfo.age %></h1>

</body>


</html>
 

[결과 출력]


반응형
Comments