node 动态页面渲染

代码:

'use strict'

const express = require('express');

const consoldiate = require('consolidate');

const app = express();

 

app.engine('html',consoldiate.ejs);//指定模板引擎

app.set('view engine','html');//指定扩展名,设置后不需要再写扩展名

app.set('views',__dirname+'/views');//指定网页资源路径

 

app.locals.group='javascript'; //网址:https://cnodejs.org/topic/5791794a4cddcb43261466cb

var dbs = {

  getData : function(req,res,next){

    const arr = [1,2,3,4,5,6,7,8,9,0];

    res.locals = {

      group : 'node.js',//如果这里没有设置,页面的group就引用之前的group属性,值也就是javascript

      name : '王',

      arr,

      testSum:function(a,b){

        return a+b;

      }

    }

  next();

  }

}

 

app.get('/',dbs.getData,function(req,res){ //数据逻辑相分离

  res.render('home');

});

app.listen(8000);

posted @ 2018-05-14 17:40  甜珊贝奇  阅读(222)  评论(0编辑  收藏  举报