템플릿 기능 정리정돈하기
객체를 이용해서 정리정돈을 해보기로 했다
var template = {
html:function(title, list, body, control){
return `
<!doctype html>
<html>
<head>
<title>WEB1 - ${title}</title>
<meta charset="utf-8">
</head>
<body>
<h1><a href="/">WEB</a></h1>
${list}
${control}
${body}
</body>
</html>
`;
}, list:function(filelist){
var list = '<ul>';
var i = 0;
while(i < filelist.length){
list = list + `<li><a href="/?id=${filelist[i]}">${filelist[i]}</a></li>`;
i = i + 1;
}
list = list+'</ul>';
return list;
}
}
html과 list를 객체화 해서 넣어주었다
이렇게 넣어주면 아래부분을 수정해줘야한다
var list = template.list(filelist);
var html = template.Html(title, list, `<h2>${title}</h2>${description}`, `<a href="/create">create</a>`);
response.writeHead(200);
response.end(html);
함수들을 다 객체로 불러오는걸로 바꿔주었다
template가 위에 있으므로 이름을 html로 바꾸어주었다
아래도 동일하게 바꾸어준다
코드를 이렇게 간결하게 하는 것(중복을 최대한 줄이기) 즉 리펙토링은 중요하다