25-Node.js学习笔记-express-app.locals对象
app.locals对象
将变量设置到app.locals对象下面,这个数据在所有的模板中都可以获取到
``` app.locals.users=[{ name:'柠檬不酸', age:20 },{ name:'西瓜很甜, age:18 }] ``` demo ``` //引入express框架 const express = require('express'); const path = require('path') //创建网站服务器 const app = express(); app.engine('art',require('express-art-template')); app.set('views',path.join(__dirname,'views')) app.set('view engine','art');app.locals.users = [{
name:'柠檬不酸',
age:18
},{
name:'柠檬不酸le',
age:19
}]
app.get('/index',(req,res)=>{
res.render('index',{
msg:'首页'
})
})
app.get('/list',(req,res)=>{
res.render('list',{
msg:'列表页'
})
})
//监听端口
app.listen(3000);
console.log('网站服务器启动成功');
//views/index.art
-
{{each users}}
- {{$value.name}} {{$value.age}} {{/each}}
-
{{each users}}
- {{$value.name}} {{$value.age}} {{/each}}