express学习12-app.locals对象
原创
©著作权归作者所有:来自51CTO博客作者前端歌谣的原创作品,请联系作者获取转载授权,否则将追究法律责任
const express = require('express');
const path = require('path');
const app = express();
app.engine('art', require('express-art-template'))
app.set('views', path.join(__dirname))
app.set('view engine', 'art');
app.locals.users = [{
name: 'zhangsan',
age: 20
}, {
name: 'geyao',
age: 20
}, ]
app.get('/index', (req, res) => {
res.render('index1', {
msg: 'message'
})
})
app.get('/list', (req, res) => {
res.render('list1', {
msg: 'list page'
})
})
app.listen(3000);
console.log('服务器启动成功');
index.art
{{msg}}
<ul>
{{each users}}
<li>
{{$value.name}}
{{$value.age}}
</li>
{{/each}}
</ul>