반응형
Notice
Recent Posts
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Today
Total
관리 메뉴

H-Log

[node.js] 경로에 따라 html파일 보내주기 본문

dev-log/server

[node.js] 경로에 따라 html파일 보내주기

hong6v6 2022. 4. 11. 15:45
반응형

이전에 적용해본 /pet, /beauty 등에 진입 시 html파일을 보여주자

 

기존에는 res.send('메세지')로 화면에 글을 보여줬는데,

html을 보여줄 때에는 res.sendFile(’파일명)을 사용한다.

// server.js

app.get('/', function(req, res){
    res.sendFile( __dirname + '/index.html')
})

__dirname은 server.js의 경로를 불러오는 것이고, server.js와 같은 경로에 있는 index.html을 불러오는 것이다.

현재는 최상단 페이지가 보여지도록 '/' 로 표기되어있다.

'/'는 최상단이라는 의미이다.

 

 

그리고 경로에 맞게 index.html을 생성해서 텍스트를 넣어보자

// index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Main</title>
</head>
<body>
    <h1>안녕하세요</h1>
    <p>홈입니다</p>
</body>
</html>

 적어보고, nodemon으로 자동 재실행 된 localhost:8080에 들어가보면 화면에 나타나고 있다.

 

 

끝! 

 

출처 : 코딩애플 유튜브

링크 : https://www.youtube.com/watch?v=Yn4fUo1i1-s

반응형
Comments