接上篇:使用express+ejs构建node app
使用上一篇写完的程序读取数据。
修改server.js
app.get('/',function(){
var drinks = [
{ name : 'Bloody Mary' , drunkness : 3 },
{ name : 'Martini' , drunkness : 5 },
{ name : 'Scotch' , drunkness : 10 }
];
var tagline = "Any code og your own that you haven't looked at for six or more months might as well have been written by someone else.";
res.render('pages/index',{
drinks:drinks,
tagline:tagline
});
});
修改index.ejs
在任何你想要显示数据的地方
<h2>Variable</h2>
<!--使用<%= Variable %>输出变量-->
<p><%= tagline %></p>
<h2>Loop</h2>
<ul>
<% drinks.forEach(function(drink) { %>
<li><%= drink.name %> - <%= drink.drunkness %></li>
<% }); %>
</ul>
over。
其实还挺简单。