如何在视图ejs中访问会话变量?

时间:2018-03-19 06:51:06

标签: node.js express ejs

用户登录后,我想在会话中保留他的详细信息,并在ejs模板中访问这些内容。我如何实现这一目标?

app.post('/registration', function(req, res) {
    var name = req.session.user_name = req.body.user_name;
    res.render('pages/registration-data');
});

如果在会话中初始化/和/或存在,我想从我拥有的所有.ejs模板中访问req.session.user_name。这是正确的方法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用app.post('/registration', function(req, res) { var name = req.session.user_name || req.body.user_name; res.locals.user_name = name; res.render('pages/registration-data'); }); 对象在会话中存储用户/会话详细信息,可以从任何模板中获取

http://expressjs.com/en/api.html#res.locals

["123","346","789"] to [[1,2,3],[4,5,6],[7,8,9]]

stringToInt :: [String] -> [[Int]]