Node.Js 部分数据

Node.Js partial data

我想在我的 Node.Js 应用程序的所有视图中访问一些默认数据。我的数据需要在 /views/layout/header.ejs

└── views
   ├── index.ejs
   └── layout
       ├── footer.ejs
       └── header.ejs

将以下中间件添加到您的主文件中。

app.use(function (req, res, next) {
      res.locals.user = req.user  //say you need loggedin user in all your views
      next();
});

在ejs中你可以在任何地方使用user,例如:

<%= user.userName %>