使用express+ejs构建node app 读取数据

接上篇:使用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

在任何你想要显示数据的地方

Variable

<%= tagline %>

Loop

    <% drinks.forEach(function(drink) { %>
  • <%= drink.name %> - <%= drink.drunkness %>
  • <% }); %>

over。
其实还挺简单。

你可能感兴趣的:(ejs,express,node.js)