Express101

March 18, 2019

Template Helpers

Express/Response Properties: res.locals

This property is useful for exposing request-level information such as the request path name, authenticated user, user settings, and so on.

app.use(function(req, res, next){
  res.locals.user = req.user;
  res.locals.authenticated = ! req.user.anonymous;
  next();
});

將 helpers 放入 res.locals

image-20200205222125349

就可以在 template 中(如 pug 中)使用 helpers

image-20200205222536843


Profile picture

Written by YU-HSIN, CHEN,
Front-end Engineer at PayPay Corporation.,
Loves pursuing modern web development.Find me on LinkedIn