2. [node.js] GET 요청시 HTML파일 보내기

jangdu·2023년 3월 19일
0

node.js

목록 보기
3/11

GET 요청시 HTML파일 보내기

누군가 우리 주소에 방문했을 때 HTML파일을 보내는 코드를 작성해보자

작성해본 server.js파일과 같은 경로에 index.html파일 생성 후 작성

<!DOCTYPE html>
<html lang="en">
  <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>Document</title>
  </head>
  <body>
    <h4>홈페이지 첫 작성 해봤어</h4>
  </body>
</html>

위처럼 작성 후 다시 server.js에 아래처럼 작성해보자

app.get('/', function(req, res) {
  res.sendFile(__dirname + '/index.html')
})
  • sendFile(): 파일을 보내는 함수
  • __dirname: 현재 파일의 경로

너무 간단하고 쉽노

profile
대충적음 전부 나만 볼래

0개의 댓글