express视图助手dynamicHelpers和helpers从2.x到3.x的变化

2.x:

app.helpers({
  config: config,
  title: config.title
});
app.dynamicHelpers({
  //防止csrf攻击
  csrf: function(req,res) {
    return req.session ? req.session._csrf : '';
  },
  req: function(req,res) {
    return req;
  },
  userInfo: function(req,res){
    return req.session.user;
  }
});

3.x

//app.helpers() 
app.locals({
  config: config,
  title: config.title
});
//app.dynamicHelpers
app.use(function(req, res, next){
  res.locals.title = config['title']
  res.locals.csrf = req.session ? req.session._csrf : '';
  res.locals.req = req;
  res.locals.session = req.session;
  next();
});
app.use(app.router);

转载自:http://www.9958.pw/post/dynamicHelpers_helpers

你可能感兴趣的:(express,helpers,dynamicHelpers)