
개인 공부를 하고있습니다. ejs 모듈을 이용하여 반복문으로 작성해 글자색이 변하도록 구문을 작성하고 싶은데 html 태그 안에는 작성이 안되는 지 해결하고 싶은데 잘 안됩니다.
파일명은 각각 test.html , test.js 입니다.
변경 전 html 파일입니다.
<% var text = "" %>
<% colors.forEach( function(once) { %>
<h2 style= <%text%> >나는 지금 ejs 모듈을 공부하고 있다.</br>그치만 어렵다.</h2>
<% }) %>
<%text.append("color : once ")%>
변경 전 JS 파일입니다.
// 서버를 생성하고 실행합니다.
http.createServer(function (request, response) {
// ejsPage.ejs 파일을 읽습니다.
fs.readFile('test.html','utf8',function (error, data) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(
'<head><meta charset="UTF-8"></head>'
);
response.end(ejs.render(data, {
colors: ['red', 'blue']
}));
});
}).listen(52274, function() {
console.log('Server Running at http://127.0.0.1:52274');
});
변경 후 html 파일입니다.
<% colors.forEach( function(color) { %>
<h2 style = <%= color %> >나는 지금 ejs 모듈을 공부하고 있다.</br>그치만 어렵다.</h2>
<% })%>
변경 후 JS 파일입니다.
http.createServer(function (request, response) {
// ejsPage.ejs 파일을 읽습니다.
fs.readFile('test.html','utf8',function (error, data) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(
'<head><meta charset="UTF-8"></head>'
);
response.end(ejs.render(data, {
colors: [
{'color': 'red'},
{'color': 'orange'},
{'color': 'yellow'},
{'color': 'green'},
{'color': 'blue'},
{'color' : 'navy'},
{'color': 'purple'},
]
}
));
});
}).listen(52273, function() {
console.log('Server Running at http://127.0.0.1:52273');
});
