템플릿 파일: DB에서 가져온 데이터 중 어떤 값을 어느 위치에 넣을지 미리 만들어 놓은 폼
템플릿 엔진: 템플릿 파일을 만들고 DB에서 가져온 동적인 데이터를 템플릿 파일에 연결해주는 역할

Node에서 앞으로 어떤 템플릿 엔진을 사용할 것인지 알려주는 과정
npm i ejs
// ejs view engine 세팅
app.set("view engine", "ejs");
app.set("views", "./views");
res.render(ejs 파일, { 변수: 전송자료})
// getAll.ejs 파일에 User List라는 텍스트를 넘겨줄 때 heading 변수 사용
res.render("getAll", { heading: "User List"} );
<%= 변수 %>
<% 자바스크립트 코드 %>
<%- HTML 코드 %>
<!-- heading 변수 값 표시 -->
<%= heading %>
<!-- 자바스크립트 코드 -->
<% if (age > 10) { %>
<p> 나이 : <%= age%></p>
<% } else { %>
<p> 나이가 너무 어립니다. </p>
<% } %>
<!-- HTML 코드 삽입 -->
<%- include('include/header') %>
contactController.js
const getAllContacts = asyncHandler(async (req, res) => {
// 전체 연락처 보기
const contacts = await Contact.find();
const users = [
{ name: "Kim", email: "kim@abc.com", phone: "12345" },
{ name: "Lim", email: "Lim@abc.com", phone: "12341235" },
];
// getAll.ejs에 user 내용 전달
res.render("getAll", { users: users });
});
getAll.ejs
<ul>
<% users.forEach( user => { %>
<li><%= user.name %></li>
<li><%= user.email %></li>
<li><%= user.phone %></li>
<% }); %>
</ul>

...
app.use(express.static("./public"));
<%- include("파일 주소")%>
<!DOCTYPE html>
<html lang="ko">
<!-- header -->
<head></head>
<body>
<main></main>
</body>
<!-- footer -->
</html>
<!DOCTYPE html>
<html lang="ko">
<!-- header -->
<%- include("./include/_header")%>
<body>
<main></main>
</body>
<!-- footer -->
<%- include("./include/_footer")%>
위와 같이 공통된 부분들을 따로 파일로 만들어 모듈화를 한 후, include를 이용해 코드의 반복을 줄일 수가 있다.
const getAllContacts = asyncHandler(async (req, res) => {
// 전체 연락처 보기
const contacts = await Contact.find();
// getAll.ejs에 user 내용 전달
res.render("index", { contacts: contacts });
});
<%- include("./include/_header")%>
<!-- Main -->
<main id="site-main">
<div class="button-box">
<a href="#" class="btn btn-light"
><i class="fa-solid fa-user-plus"></i>연락처 추가</a
>
</div>
<table class="table">
<thead>
<tr>
<th>이름</th>
<th>메일 주소</th>
<th>전화번호</th>
<th> </th>
</tr>
</thead>
<tbody>
<% contacts.forEach( contact => { %>
<tr>
<td><%= contact.name%></td>
<td><%= contact.email%></td>
<td><%= contact.phone%></td>
<td>
<a href="#" class="btn update" title="수정">
<i class="fas fa-pencil-alt"></i>
</a>
<a href="#" class="btn delete" title="삭제">
<i class="fas fa-times"></i>
</a>
</td>
</tr>
<% }) %>
</tbody>
</table>
</main>
<!-- /Main -->
<%- include("./include/_footer")%>
