if(queryData.id === undefined){ /* fs.readdir('./data', function(error, filelist){ var title = 'Welcome'; var description = 'Hello, Node.js'; 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); }); */ db.query(`SELECT * FROM topic`, function(error, contents){ var title = 'Welcome'; var description = 'Hello, Node.js'; var list = template.list(contents); var html = template.HTML(title, list, `<h2>${title}</h2>${description}`, `<a href="/create">create</a>`); response.writeHead(200); response.end(html); }); }
db.query() 함수의 콜백함수 인자로 들어오는 contents 변수에 mysql query 결과가 배열 형태로 들어오게됩니다.
때문에 /lib/template.js 의 template.list() 함수를 조금 수정해줬습니다.
변경된 template.js
1 2 3 4 5 6 7 8 9 10
list:function(contents){ var list = '<ul>'; var i = 0; while(i < contents.length){ list = list + `<li><a href="/?id=${contents[i].id}">${contents[i].title}</a></li>`; // query 결과가 배열 형태로 오므로 수정 i = i + 1; } list = list+'</ul>'; return list; }