[Restful] REST - 주석 Index

Zero·2023년 3월 13일
0

REST

목록 보기
2/8

Index

index를 통해 데이터를 조회하는 기능 즉, CRUD 중 READ를 구현해보도록 하자.

ex) resource : comments

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

app.use(express.urlencoded( {extended: true}))
app.use(express.json())
app.set('view engine', 'ejs');
app.set('views',path.join(__dirname,'views'));

const comment = [
  {
    username : 'Todd',
    comment : 'lol that is so funny !'
  },
  {
    username : 'Skyler',
    comment : 'I like to go birdwatching with my dog'
  },
  {
    username : 'Sk8erBoi',
    comment : 'Plz delete your account, Todd'
  },
  {
    username : 'onlysayswoof',
    comment : 'woof woof woof !!'
  }
]

app.get('/comments', (req,res) => {
  res.render('comments/index', { comment });
})

app.listen(3000, () => {
  console.log("Listen 3000 port !!!");
})

  • views/comments 폴더내에 index.ejs 템플릿을 생성해주자
<!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>Comments Index</title>
</head>

<body>
  <h1>Comments</h1>
  <ul>
    <% for(let c of comment) {%>
      <li>
        <%= c.comment%> <b>
            <%= c.username %>
          </b>
      </li>
      <% } %>

  </ul>
</body>

</html>

해당 코드를 살펴보면, 파일명은 index.ejs이며, comment 데이터를 express를 통해 전달받아 ejs 문법을 통해 화면단에 출력해준다.

/comment resource를 통해 GET 요청으로 데이터 조회 기능을 구현한 것이다.


--> 실행 결과

0개의 댓글