Node.Js部分数据

用户名

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

└── views
   ├── index.ejs
   └── layout
       ├── footer.ejs
       └── header.ejs
Talha Awan |

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

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 %>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章