Express 2.x -> 3.x 视图助手注册方法

2.x:

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

3.x

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

你可能感兴趣的:(Express 2.x -> 3.x 视图助手注册方法)